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.

很有难度的一道题,不看讨论几乎没法accept。

解法看来看去基本就是dp,与一般dp不一样的是,需要对两个特征进行dp记录。

1.使用dp[i]记录s.substring(0,i)的分割数,初始值为dp[i]=i-1. 0<=i<=s.length().

对于每个i, 可以找到至少一个j, 0<=j<=i-1, 使得s.substring(j,i)是回文字符串。找到所有这样的j的集合J。转移函数即为: dp[i] = min(dp[j]+1) (j∈J)

仅仅这么做还是会超时,那么还得继续优化。当前算法的重复处在于,每次判断s.substring(j,i)是否为回文字符串时,都需要遍历这个字符串,所以还需要第二个dp记录s.substring(j,i)是否为回文字符串。

2. 使用isP[i][j]记录s.substring(i,j)是否为回文字符串。0<=i<=s.length(),0<=j<=s.length(). 初始状态isP[i][i]=true (0<=i<=s.length()), isP[i][i+1]=true (0<=i<s.length()).

(PS,后面代码中没有初始化isP[i][i+1],因为在遍历过程中作特殊判断了(i-j<2时必定为真))

这样的话要判断s.substring(i,j)是否为回文字符串,只需要isP[i+1][j-1]为真且s.charAt(i-1)==s.charAt(j).

同时我们也可以明白,遍历的顺序应该是逐渐将i,j距离拉长的。

所以应该有两层循环,第一层循环是用来记录dp[i], i从1到s.length().

第二层循环记录isP[j][i](i已固定), j从i-1到0,反向遍历。

最后dp[s.length()]即为结果

代码如下:

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

[Leetcode][JAVA] Palindrome Partitioning II的更多相关文章

  1. 【leetcode】Palindrome Partitioning II

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

  2. 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 ...

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

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

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

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

  5. Leetcode 132. Palindrome Partitioning II

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

  6. leetcode@ [131/132] Palindrome Partitioning & Palindrome Partitioning II

    https://leetcode.com/problems/palindrome-partitioning/ Given a string s, partition s such that every ...

  7. [LeetCode] 131. Palindrome Partitioning 回文分割

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

  8. LeetCode:Palindrome Partitioning,Palindrome Partitioning II

    LeetCode:Palindrome Partitioning 题目如下:(把一个字符串划分成几个回文子串,枚举所有可能的划分) Given a string s, partition s such ...

  9. [LeetCode] Palindrome Partitioning II 解题笔记

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

随机推荐

  1. js显示当前时间

    闲着没事在闪存里看到有人需要js显示当前时间,就一时兴起写了个. 输出格式:“2013年12月18日 星期三 上午9:05:00 ”. <script type="text/javas ...

  2. CSS3 初步学习

    CSS3有一些是与旧版CSS2.1重叠的,有一些是没有浏览器支持的,全学没必要,下面只记录一下有用的. 一.CSS3边框 1.圆角border-radius border-radius:值越大,角越圆 ...

  3. C++学习基础十——子类构造函数与析构函数的执行

    1.子类构造函数的执行: 先执行父类的构造函数,再执行成员对象的构造函数,最后执行自身的构造函数. 当继承多个类时,构造函数的 执行顺序与继承时的顺序 相同,而与子类构造函数调用父类构造函数的顺序无关 ...

  4. 判断 0 和 '' 以及 empty null false的关系

    if('safdasefasefasf'==0) { echo "该字符串转换为数字 等于 0 <br/>"; } //output:该字符串转换为数字 等于零. 这是 ...

  5. js 小工具-- 获取主机名

    <script type="text/javascript"> function getHostName(url) { var host = "null&qu ...

  6. C# WinForm 单例模式(例:同一个窗体只创建一次实例)

    //C# WinForm 单例模式(例:同一个窗体只创建一次实例) //打开窗体的事件: Form3 f = Form3.InstanceObject() ; //实例化窗体 f.Focus(); / ...

  7. Fiddler抓包测试App接口

    Fiddler抓包测试App接口 使用Fiddler对手机App应用进行抓包,可以对App接口进行测试,也可以了解App传输中流量使用及请求响应情况,从而测试数据传输过程中流量使用的是否合理. 抓包过 ...

  8. sqlserver 加内置dll的使用内存

  9. 8.10 CSS知识点3

    7.属性选择符 选择符 版本 描述 E[att] CSS2 选择具有att属性的E元素 E[att="val"] CSS2 选择具有att属性值等于val的E元素 E[att~=& ...

  10. 添加图片按钮-UI界面编辑器(SkinStudio)教程

    打开工具箱,选择Button控件 在窗体上添加一个Button控件 给Button控件的ImageNormal属性添加图片 添加完成后的效果 删除Text属性的值(即删除显示的文本)