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.
链接: http://leetcode.com/problems/palindrome-partitioning-ii/
题解:
一上来思路是在Palindrome Partition I的基础上,找到所有的Partition,然后再查找一遍最短的解,这样做会超时。 接下来又想可不可以使用suffix array / suffix tree,或者Manancher。最后想来想去老老实实看discuss去。大部分还是使用dp - 初始化,找到转移方程,然后计算。 主要参考了小莹子和水中的鱼大神。
初始定义一个一维数组minCuts,用来表示s.substring(0, i + 1)时的最小cut数。
再定义一个二维布尔数组isPalindrome,用来表示 s.substring(j, i)是否是palindrome。
核心算法是,假如isPalindrome[j][i]是palindrome,说明 j - 1至i + 1只需要1个cut, 因此对每一个i, minCuts[i]可以进行更新 - 比较现有 minCuts[i] 与 minCuts[j - 1] + 1。 这里也是一个一维的dp。
而另外一部分,isPalindrome[j][i]是palindrome的条件是 isPalindrome[j + 1,i - 1]为true而且s.charAt(j) == s.charAt(i)。举个例子,字符串"abba", j = s.charAt(0), i = s.charAt(3), 则"abba"为true的条件是"bb"为palindrome而且'a' == 'a'。 其中 j + 1 = s.charAt(1), i - 1 = s.charAt(2)。即 j + 1, i - 1代表当前字符串内部1个单位的子串。
特殊情况是 i - j <= 1时,isPalindrome[j,i]也为true。因为 i - j = 0时,i = j, 此时s.substring(j,i)是一个字符,所以为true。 而i - j = 1时,类似于“bb”, 此时是偶数Palindrome,所以值也为true。 (其实情况可以扩大到i - j <= 2, 这时 s.charAt(i) == s.charAt(j),类似于"aba",可以忽略中间的字符,也是一个Palindrome,不过对结果没有影响, 因为这种情况会被 i - j = 0 包括)
对每个i来说, 一开始可以初始化minCuts[i] = Integer.MAX_VALUE,或者 minCuts[i] = i。 每次判断到isPalindrome[j][i]为真时,尝试更新 minCuts[i]。 这里的特殊情况是,当j = 0时, 说明 0 至 i + 1为palindrome,此时不需要cut,所以设置 minCuts[i] = 0。
Time Complexity - O(n2), Space Complexity - O(n2)。
public class Solution {
public int minCut(String s) {
int len = s.length();
int[] minCuts = new int[len]; //minCuts[i] is min cut for s.substring(0, i + 1)
boolean[][] isPalindrome = new boolean[len][len]; for (int i = 0; i < len; i++) {
minCuts[i] = Integer.MAX_VALUE; //set initial value for minCuts[i] for (int j = 0; j <= i; j++) {
if (s.charAt(i) == s.charAt(j)) { //if s.substring(j, i) is Palindrome
if (i - j <= 1 || isPalindrome[j + 1][i - 1]) {
isPalindrome[j][i] = true;
if (j == 0)
minCuts[i] = 0; //if(s[0....i] is palindrome), no cut needed
else {
minCuts[i] = Math.min(minCuts[i], minCuts[j - 1] + 1); //1-D dp
}
}
}
}
}
return minCuts[len - 1];
}
}
Discussion里还有更好的写法,只用O(n)的空间复杂度,第二遍刷时要好好研究。
题外话: 周6去爬山,break neck trail, 在熊山附近。 最近缺乏运动,体力不是很好,在山顶吹了会小风就有点感冒了,现在头还很痛。周日去找赵师傅,把装修定金取了回来。连续两天奔波很累。这个周末也没有学习,倒是把<三体>三部曲全看完了,写得确实好。 接下来要好好休息,好好刷题。
Reference:
http://www.cnblogs.com/springfor/p/3891896.html
http://www.programcreek.com/2014/04/leetcode-palindrome-partitioning-ii-java/
http://fisherlei.blogspot.com/2013/03/leetcode-palindrome-partitioning-ii.html
http://blog.csdn.net/ljphhj/article/details/22573983
http://blog.csdn.net/yuanhisn/article/details/46117525
https://leetcode.com/discuss/9476/solution-does-not-need-table-palindrome-right-uses-only-space
http://blog.csdn.net/ljphhj/article/details/22799189
https://leetcode.com/discuss/6691/my-dp-solution-explanation-and-code
https://leetcode.com/discuss/33077/solved-shortest-path-algorithm-clear-and-straightforward
https://leetcode.com/discuss/47140/two-versions-given-one-28ms-one-manancher-like-algorithm-10
132. Palindrome Partitioning II的更多相关文章
- 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 132. Palindrome Partitioning II ----- java
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- 132. Palindrome Partitioning II (String; DP)
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- 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 ...
- Leetcode 132. Palindrome Partitioning II
求次数的问题一般用DP class Solution(object): def minCut(self, s): """ :type s: str :rtype: int ...
- 动态规划之132 Palindrome Partitioning II
题目链接:https://leetcode-cn.com/problems/palindrome-partitioning-ii/description/ 参考链接:https://blog.csdn ...
- 【leetcode dp】132. Palindrome Partitioning II
https://leetcode.com/problems/palindrome-partitioning-ii/description/ [题意] 给定一个字符串,求最少切割多少下,使得切割后的每个 ...
- 132 Palindrome Partitioning II 分割回文串 II
给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串.返回 s 符合要求的的最少分割次数.例如,给出 s = "aab",返回 1 因为进行一次分割可以将字符串 s 分 ...
随机推荐
- Android屏幕保持唤醒状态
我们程序偶尔会有需要屏幕一直或较长时间的保持唤醒状态,而用户的睡眠时间又设置的比较短.这时可能会对程序以及用户的使用造成一定的影响.在Android中有两种方法,可以让我们在我们需要保持唤醒的页面长时 ...
- ASP.NET验证控件详解
现在ASP.NET,你不但可以轻松的实现对用户输入的验证,而且,还可以选择验证在服务器端进行还是在客户端进行,再也不必考虑那么多了,程序员们可以将重要精力放在主程序的设计上了. ASP.NET公有六种 ...
- Java多线程间通信-解决安全问题、等待唤醒机制
/*1.增加一个知识点一个类怎么在所有的类中,让其它类来共同修改它的数据呢?可以用单例设计模式可以用静态可以在其它类中做一个构造函数,接受同一个对象,这样就可以实现对象 2.状态选择可以用数字0 1 ...
- java培训(1-4节课)
课程安排:JavaEE方向(控制台程序,GUI程序,Web程序,手机程序)(dos命令是控制台程序:QQ是GUI程序,放在计算机上:QQ空间是Web程序,放在腾讯公司) 讲课的13本教材:C语言,Ja ...
- PHPstrom 增加emmet插件
之前记得使用Eclipse的时候有一个插件叫 emmet 可以实现快速开发前端,简直就是前端开发秒杀神器: 输入对应的代码一个table键就搞定了一堆代码: 在emmet的官网上 看到其实是支持的PH ...
- 【HeadFirst设计模式】7.适配器模式与外观模式
今晚学习完第七章,顺便做一下知识备忘. 适配器模模式: 定义:将一个类的接口,转换成客户期望的另一个接口.适配器让原本接口不兼容的类可以合作无间. 对象适配器: 类适配器: 外观模式: 提供了一个统一 ...
- Python3 多进程和多线程
Unix/Linux操作系统提供了一个fork()系统调用,它非常特殊.普通的函数调用,调用一次,返回一次,但是fork()调用一次,返回两次,因为操作系统自动把当前进程(称为父进程)复制了一份(称为 ...
- Entity Framework 插入数据 解决主键非自增问题
http://blog.csdn.net/educast/article/details/8632806 与Entity Framework相伴的日子痛并快乐着.今天和大家分享一下一个快乐,两个痛苦. ...
- 百度bae定时任务使用方法
最近想做个定时执行某些请求的任务,因为不是java的,不能有常住内存的控制,php不知百度bae云怎么做,找了很久终于被我找到了 https://cloud.baidu.com/doc/BAE/GUI ...
- Java中的Inner Class (一)
Inner Class看起来是一个简单的Code-Hideing机制,但是Java的Inner Class和C++的有所不同 - Inner Class能够和外部类(Surrounding Class ...