Java for LeetCode 132 Palindrome Partitioning II
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的更多相关文章
- leetcode 132. Palindrome Partitioning II ----- java
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- Leetcode 132. Palindrome Partitioning II
求次数的问题一般用DP class Solution(object): def minCut(self, s): """ :type s: str :rtype: int ...
- leetcode 131. Palindrome Partitioning 、132. Palindrome Partitioning II
131. Palindrome Partitioning substr使用的是坐标值,不使用.begin()..end()这种迭代器 使用dfs,类似于subsets的题,每次判断要不要加入这个数 s ...
- 【LeetCode】132. Palindrome Partitioning II
Palindrome Partitioning II Given a string s, partition s such that every substring of the partition ...
- 【leetcode】Palindrome Partitioning II
Palindrome Partitioning II Given a string s, partition s such that every substring of the partition ...
- 【leetcode】Palindrome Partitioning II(hard) ☆
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- 132. Palindrome Partitioning II
题目: Given a string s, partition s such that every substring of the partition is a palindrome. Return ...
- Java for LeetCode 131 Palindrome Partitioning
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- [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 ...
随机推荐
- win7 32位用pyinstaller打包Python和相关html文件 成exe
http://tieba.baidu.com/p/3060401749?traceid= 安装 pyinstaller 然后 第一步你的脚本里面要做相应处理,添加一个函数:def resource_p ...
- Upan
http://www.xiazaijidi.com/ http://www.ushendu.com/
- springboot + mybatis配置多数据源示例
转:http://www.jb51.net/article/107223.htm 在实际开发中,我们一个项目可能会用到多个数据库,通常一个数据库对应一个数据源. 代码结构: 简要原理: 1)Datab ...
- 线程安全的概念和Synchronized(读书笔记)
并行程序开发的一大关注重点就是线程安全,一般来说,程序并行化为了获取更多的执行效率,但前提是,高效率不能以牺牲正确性为代价,线程安全就是并行程序的根本和根基.volatile并不能真正保证线 ...
- Putty的噩梦——渗透工具PuttyRider使用心得分享
我们在入侵到一台主机的时候,经常会看到管理员的桌面会放着putty.exe,这说明有很大的可能性管理员是使用putty远程管理主机的. 该工具主要是针对SSH客户端putty的利用,采用DLL注入的方 ...
- HTML5-SQLLite连接
1.代码部分(可直接粘贴到html文件中运行) <body onload="init()"> 姓名:<input type="text" id ...
- jQuery与ajax的应用(一)
<body> <div id="resText"></div> <div id="reshtml"></d ...
- 制作个人开发IDE
1.打开VS2013,新建项目: 2.点击下一步,下一步.到达例如以下界面: 3.下一步 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdG90b3R ...
- 移动应用开发测试工具Bugtags集成和使用教程【转载】
前段时间,有很多APP突然走红,最终却都是樱花一现.作为一个创业团队,突然爆红是非常难得的机会.然并卵,由于没有经过充分的测试,再加上用户的激增,APP闪退.服务器数据异常等问题就被暴露出来,用户的流 ...
- 推荐TED演讲:20岁光阴不再来(Why 30 is not the new 20)
缘起 早上起来在电脑上看到"自强不息"群(群号)中骆宏给大家分享的视频."20岁光阴不再来",利用短暂的时间浏览了一下.就像把这个TED视频分享给很多其它的朋友 ...