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.


题解:DP。

用dp[i]记录从[i,s.length]所需要的最少cut,则dp[i] = min(dp[i], dp[j+1]+1) (j=i,i+1,...,s.length)。

即如果s[i,j]是一个回文字符串,那么s[j+1,s.length]需要的最少cut,加上s[i,j]就可以得到一个cut。

如下图所示:

此题还有个坑是判断回文的时候,不能用单独的判断函数,而要在动态规划的过程中维护一个数组isPar[i][j]表示s[i,j]是否是字符串,因为我们只在s[i,j]是回文的时候才去看dp[j+1]并且得到一个cut,所以我们只需要在s(i) == s(j)的时候,利用isPar[i+1,j-1](或者j-i<2)做出s[i,j]是否是回文的判断。

代码如下:

 public class Solution {
public int minCut(String s){
if(s == null || s.length() == 0)
return 0;
int length = s.length();
int[] dp = new int[length+1];
boolean[][] isPar = new boolean[length][length]; for(int i = length-1;i>=0;i--){
dp[i] = s.length() - i;
for(int j = i;j<s.length();j++){
//only if s[i] == s[j], s[i,j] may be a palindrome
if(s.charAt(i)==s.charAt(j)){
if(j-i<2 || isPar[i+1][j-1]){
isPar[i][j] = true;
dp[i] = Math.min(dp[i], dp[j+1]+1);
}
}
}
}
return dp[0]-1;
}
}

【leetcode刷题笔记】Palindrome Partitioning II的更多相关文章

  1. 【leetcode刷题笔记】Permutations II

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  2. 【leetcode刷题笔记】Subsets II

    Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...

  3. 【leetcode刷题笔记】N-Queens II

    Follow up for N-Queens problem. Now, instead outputting board configurations, return the total numbe ...

  4. LeetCode刷题笔记和想法(C++)

    主要用于记录在LeetCode刷题的过程中学习到的一些思想和自己的想法,希望通过leetcode提升自己的编程素养 :p 高效leetcode刷题小诀窍(这只是目前对我自己而言的小方法,之后会根据自己 ...

  5. 【leetcode刷题笔记】Palindrome Partitioning

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

  6. LeetCode刷题笔记 - 12. 整数转罗马数字

    学好算法很重要,然后要学好算法,大量的练习是必不可少的,LeetCode是我经常去的一个刷题网站,上面的题目非常详细,各个标签的题目都有,可以整体练习,本公众号后续会带大家做一做上面的算法题. 官方链 ...

  7. 18.9.10 LeetCode刷题笔记

    本人算法还是比较菜的,因此大部分在刷基础题,高手勿喷 选择Python进行刷题,因为坑少,所以不太想用CPP: 1.买股票的最佳时期2 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. ...

  8. Leetcode刷题笔记(双指针)

    1.何为双指针 双指针主要用来遍历数组,两个指针指向不同的元素,从而协同完成任务.我们也可以类比这个概念,推广到多个数组的多个指针. 若两个指针指向同一数组,遍历方向相同且不会相交,可以称之为滑动窗口 ...

  9. 【leetcode刷题笔记】Word Ladder II

    Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...

随机推荐

  1. 更改centos 7 的默认启动为命令界面

    vi /etc/inittab 可以看到这样两行: # To set a default target, run:# systemctl set-default TARGET.target 故: su ...

  2. 微信 oauth授权2

    2.前面请求成功后 会在跳转url后得到 ?code=00b788e3b42043c8459a57a8d8ab5d9f&state=1 3.之后 使用code换取access_token 换取 ...

  3. asp.net 页面延时五秒,跳转到另外的页面

    asp.net 页面延时五秒,跳转到另外的页面的实现代码. --前台 <%@ Page Language="C#" AutoEventWireup="true&qu ...

  4. MFC中调用chm帮助文档(使用相对路径)

    主要就一句话: ShellExecute(NULL,L"Open",path,NULL,NULL,SW_SHOWMAXIMIZED); 常用的显示方式是SW_SHOWNORMAL和 ...

  5. ApiDoc 和 Swagger 接口文档

    ApiDoc:https://blog.csdn.net/weixin_38682852/article/details/78812244 Swagger git: https://github.co ...

  6. Tomcat虚拟目录

    x先来看一段server.xml文件里的配置: <Host appBase="" autoDeploy="true" debug="0" ...

  7. sedna进行xquery查询

    有一个文件book.xml: <books> <book> <name>The Call Of Wild</name> <author>Ja ...

  8. linux 下Shell编程(四)

    for循环应用实例 for 循环可以对一个记录集中地数据依次集中地数据依次进行处理. #!/bin/bash #4.13.sh for clear     #看下图区别 for((i=1:i<1 ...

  9. linux 下 Shell编程(三)

    if语句应用实例 if语句可以在程序中实现各种逻辑判断. 用if语句判断并显示文件的信息 可以用test命令和相关的参数来判断文件的属性,然后根据判断结果输出文件的信息. #!/bin/bash #4 ...

  10. 《PhotoShop CS6 》第一节 矢量与分辨率

    分辨率:不一定是方形,可以调整其比例. 色彩模型:色相Hue(圆周,冷暖相接),饱和度Saturation(半径),明度Brightness(轴,从黑到白).