Given a string s, partition s such that every substring of the partition is a palindrome.

Return the minimum cuts needed for a palindrome partitioning of s.

For example, given s = "aab",
Return 1 since the palindrome partitioning ["aa","b"] could be produced using 1 cut.

解题思路:

因为是Hard,用上题的结果计算肯定是超时的,本题需要用dp的思路,开一个boolean[][]的数组计算i-j是否为palindrome,递推关系为s.charAt(j) == s.charAt(i) &&  isPal[j + 1][i - 1]) → isPal[j][i] = true,同时dp[i] = Math.min(dp[i], dp[j - 1] + 1),JAVA实现如下:

    public int minCut(String s) {
int[] dp = new int[s.length()];
for (int i = 0; i < dp.length; i++)
dp[i] = i;
boolean isPal[][] = new boolean[s.length()][s.length()];
for (int i = 1; i < s.length(); i++)
for (int j = i; j >= 0; j--)
if (s.charAt(j) == s.charAt(i)
&& (j + 1 >= i - 1 || isPal[j + 1][i - 1])) {
isPal[j][i] = true;
dp[i] = j == 0 ? 0 : Math.min(dp[i], dp[j - 1] + 1);
}
return dp[dp.length - 1];
}

Java for LeetCode 132 Palindrome Partitioning II的更多相关文章

  1. leetcode 132. Palindrome Partitioning II ----- java

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

  2. Leetcode 132. Palindrome Partitioning II

    求次数的问题一般用DP class Solution(object): def minCut(self, s): """ :type s: str :rtype: int ...

  3. leetcode 131. Palindrome Partitioning 、132. Palindrome Partitioning II

    131. Palindrome Partitioning substr使用的是坐标值,不使用.begin()..end()这种迭代器 使用dfs,类似于subsets的题,每次判断要不要加入这个数 s ...

  4. 【LeetCode】132. Palindrome Partitioning II

    Palindrome Partitioning II  Given a string s, partition s such that every substring of the partition ...

  5. 【leetcode】Palindrome Partitioning II

    Palindrome Partitioning II Given a string s, partition s such that every substring of the partition ...

  6. 【leetcode】Palindrome Partitioning II(hard) ☆

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

  7. 132. Palindrome Partitioning II

    题目: Given a string s, partition s such that every substring of the partition is a palindrome. Return ...

  8. Java for LeetCode 131 Palindrome Partitioning

    Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...

  9. [LeetCode] 132. Palindrome Partitioning II_ Hard tag: Dynamic Programming

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

随机推荐

  1. mysql count(*) 和count(1)区别

    count *更快, 不要加where,否则同count(1)效率相同 sql语句对大小写不敏感,关键字一般大写,其他小写, count(*)不加where,mysql会直接返回总条数,因为mysql ...

  2. http各类攻击及tcpcopy工具

    1.专业的还得ixia.Spirent TestCenter等软硬件一体的 2.一般的使用软件的,安装在linux上使用 参考: 1.http://blog.csdn.net/wuzhimang/ar ...

  3. 同步数据库到Codis代码

    同步mysql数据库到codis缓存中 public void syncRule() { // 根据时间戳获取Mycat中规则表数据 logger.info("start ..." ...

  4. PHP 变量定义及使用

    php的变量前面必须有$符号,而且是解释型的弱类型语言,定义的时候不需要定义变量值的类型. $str="这是个变量"; 1.输出的时候可以用拼接字符串的方法 如:echo" ...

  5. suid sgid sbit chattr lsattr find

    suid 一般用于二进制可执行文件不可用于shell脚本和目录,suid代表当用户执行此二进制文件时,暂时具有此文件所有者的权限 chmod 4xxx binfile sgid 一般用于目录,sgid ...

  6. Python & Django & Pycharm 安装

    一.下载安装Python 从https://www.python.org/上下载 Python 2.7.6,双击安装包开始安装: 单击“Next”按钮,进入Python安装组件选择界面.这里我们安装全 ...

  7. robotframework使用之元素定位动态ID方法

    转自: http://blog.csdn.net/u011757108/article/details/53418671 一个弹出框所有元素ID竟然的动态的,关闭后再打开,里面的ID又变! 如下图:  ...

  8. CPU调度算法

    批处理系统中的调度算法: *需要考虑的因素: 1. 吞吐量 2. cpu利用率 3. 周转时间 4. 公平性* 1.先来先服务: FCFS: 优点:实现简单 缺点:可能造成周转时间长 2.最短作业优先 ...

  9. 浅谈java反序列化工具ysoserial

    前言 关于java反序列化漏洞的原理分析,基本都是在分析使用Apache Commons Collections这个库,造成的反序列化问题.然而,在下载老外的ysoserial工具并仔细看看后,我发现 ...

  10. java操作pdf

    使用pdf模板生成pdf 1,工具 Adobe Acrobat X Pro 2,pom文件配置 <dependency> <groupId>com.itextpdf</g ...