题目链接:https://leetcode-cn.com/problems/palindrome-partitioning-ii/description/

参考链接:https://blog.csdn.net/jingsuwen1/article/details/51934277

dp[i]存放[0,i)即以前i个字符串的子串的最小切割数,则所求为dp[s.length()];

前0个字符串和1个字符串不需要切割,所以dp[0]=0,dp[1]=0;

1.初始化:当字串s.substring(0,i)([0,i)个字符)是回文时,dp[i] = 0(表示不需要分割);否则,dp[i] = i-1(表示至多分割i-1次);

比如abc最多切割2次(a|b|c),aa不需要切割
2.对于任意大于1的i,如果s.substring(j,i)( 1 =< j <=  i ,即遍历i之前的每个子串)是回文时,dp[i] = min(dp[i], dp[j]+1);
   (注:j不用取0是因为若j == 0,则又表示判断(0,i))。

public int minCut(String s) {
if(s == null||s.length() == 0)
return 0;
int[] dp=new int[s.length()+1];
dp[0]=0;//空字符,不需要切割
dp[1]=0;
for (int i = 2; i < dp.length; i++) {
dp[i]=is_palindrome(s.substring(0,i))?0:i-1;
}
for(int i=2;i<=s.length();i++)
{
// 1=<j<=i的子串回文判定
for(int j=i;j>=1;j--)
{
if(is_palindrome(s.substring(j,i)))
{
dp[i]=Math.min(dp[i],dp[j]+1);
}
}
}
return dp[s.length()];
}
//判断回文串例程
public boolean is_palindrome(String s)
{
//System.out.println(s);
StringBuilder sb=new StringBuilder(s);
return s.equals(sb.reverse().toString());
}

上述的dp方法在leetcdoe上显示超时,在牛客网上可以通过。所以期待更加完全的dp方法。

动态规划之132 Palindrome Partitioning II的更多相关文章

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

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

  2. 【LeetCode】132. Palindrome Partitioning II

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

  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. 132. Palindrome Partitioning II

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

  5. 132. Palindrome Partitioning II (String; DP)

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

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

  7. Leetcode 132. Palindrome Partitioning II

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

  8. 【leetcode dp】132. Palindrome Partitioning II

    https://leetcode.com/problems/palindrome-partitioning-ii/description/ [题意] 给定一个字符串,求最少切割多少下,使得切割后的每个 ...

  9. 132 Palindrome Partitioning II 分割回文串 II

    给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串.返回 s 符合要求的的最少分割次数.例如,给出 s = "aab",返回 1 因为进行一次分割可以将字符串 s 分 ...

随机推荐

  1. Cocos Creator实现的《点我+1》

    一.前言 在学习Cocos中,需要一些东西来练手,于是前段时间就开发仿照一款公司之前的产品<点我+1>来做,仿照过程中,所有的算法逻辑都是自己研究的,并没有参考公司代码,也没有使用公司的美 ...

  2. 6.短信验证码60s倒计时

    短信验证码60s倒计时 html: <input type="button"  class="btn btn-primary" value="免 ...

  3. vue-lazyload的使用

    1.下载依赖 npm install vue-lazyload --save 2.引入 import Vue from 'vue' import App from '@/App' import Vue ...

  4. vue中使用ckeditor

    1.第一步首先去ckeditor官网去下载editor文件,这里以ckeditor4为例 首先在index.html里引入js代码 <script type="text/javascr ...

  5. VS基本学习之(变量与常量)

    一.变量与常量 1) 变量        由(定义+赋值+取值组成) 变量的命名规则: ① 变量名组成:字母  数字  下划线  @  汉字 ② 首字母只能用:字母  下划线  @  汉字(不能是数字 ...

  6. caffe深度学习进行迭代的时候loss曲线开始震荡原因

    1:训练的batch_size太小 1.       当数据量足够大的时候可以适当的减小batch_size,由于数据量太大,内存不够.但盲目减少会导致无法收敛,batch_size=1时为在线学习. ...

  7. Oralce安装、使用过程中出现的问题

    OracleDBControl启动失败Unable to determine local host from URL REPOSITORY_URL=http://your-url.co 解决方法 打开 ...

  8. windows系统快捷键

    1.我的键盘:windows键的开启,需要按住FN键+windows键. 2.windows键 + E,表示打开我的电脑. 3.windows键 + R,打开windows的命令行窗口. 4.wind ...

  9. 学习笔记<1>技术体系结构

    Android的系统架构采用了分层架构的思想,如上图所示.从上层到底层共包括四层,分别是   1.应用程序程序层   2.应用框架层   3.系统库和Android运行时 4.Linux内核.   每 ...

  10. 不能安装vmtools解决:一个命令安装

    https://blog.csdn.net/fly66611/article/details/77994339 换好源 sudo su apt-get update apt-get dist-upgr ...