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.

标签: Dynamic Programming

分析:动态规划,设f[i,j]表示区间[i,j]的最少cut数,所以状态方程为:

      f[i,j]=min(f[i,k],f[k+1,j])   (i<=k<=j);

在二维dp的基础上在优化成一维dp:

设f[i]表示i到len-1的最少cut数,所以状态方程为;

      f[i]=min(f[i],f[j+1]+1)   (s[i...j]为回文字符串&&i<=j<<len-1)

判断s[i...j]是否为回文字符串也可以用动态规划,可以新建boolean数组isPal[len][len],isPal[i][j]表示s[i][j]为回文字符串;

参考代码:

 public class Solution {
public int minCut(String s) {
int len=s.length();
int cutnum[]=new int[len+1];
boolean isPal[][]=new boolean[len][len];
for(int i=0;i<=len;i++){
cutnum[i]=len-1-i;//先假设i到len-1间每个字符都cut
}
for(int i=len-1;i>=0;i--){
for(int j=i;j<len;j++){
if(s.charAt(i)==s.charAt(j)&&(j-i<2||isPal[i+1][j-1])){
isPal[i][j]=true;
cutnum[i]=Math.min(cutnum[i], cutnum[j+1]+1);
}
}
}
return cutnum[0];
}
}

LeetCode-Palindrome Partitioning II[dp]的更多相关文章

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

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

  2. LeetCode: Palindrome Partitioning II 解题报告

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

  3. [LeetCode] Palindrome Partitioning II 拆分回文串之二

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

  4. [leetcode]Palindrome Partitioning II @ Python

    原题地址:https://oj.leetcode.com/problems/palindrome-partitioning-ii/ 题意: Given a string s, partition s  ...

  5. Leetcode: Palindrome Partitioning II

    参考:http://www.cppblog.com/wicbnu/archive/2013/03/18/198565.html 我太喜欢用dfs和回溯法了,但是这些暴力的方法加上剪枝之后复杂度依然是很 ...

  6. LeetCode:Palindrome Partitioning,Palindrome Partitioning II

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

  7. 【leetcode】Palindrome Partitioning II

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

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

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

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

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

  10. 【LeetCode】132. Palindrome Partitioning II

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

随机推荐

  1. c++字符串的输入的思考

    字符串的输入,是学习c++的一个重点,也是一个极富有细节意味的知识点,如果你不了解这些细节,你可能会在写程序时犯错而一脸懵逼不知所措. 与此同时,我们要了解c++缓冲区的概念,程序的输入都建有一个缓冲 ...

  2. JS读写浏览器cookie及读取页面参数

    JS读写浏览器cookie及读取页面参数 var zbrowser = { //设置浏览器cookie,exdays是cookie有效时间 setCookie: function (c_name, v ...

  3. Python入门(2)

    一.    基础语法 1.Print print 是 python 里很基本很常见的一个操作,它的操作对象是一个字符串. 直接在 print 后面加一段文字来输出的话,需要给文字加上双引号或者单引号. ...

  4. [leetcode-551-Student Attendance Record I]

    You are given a string representing an attendance record for a student. The record only contains the ...

  5. 遇到ANDROID “call to opengl es api with no current context”错误

    延迟线程执行 Timer timer=new Timer();//实例化Timer类 timer.schedule(new TimerTask(){ public void run(){ buyed( ...

  6. 数据结构之网络流入门(Network Flow)简单小节

    网络流的相关定义: 源点:有n个点,有m条有向边,有一个点很特殊,只出不进,叫做源点. 汇点:另一个点也很特殊,只进不出,叫做汇点. 容量和流量:每条有向边上有两个量,容量和流量,从i到j的容量通常用 ...

  7. 把本地git仓库中的项目引入到码云上

    一.安装git软件和TortoiseGit客户端(git需配置环境变量,但安装时已经配置好,无需考虑) 二.生成公钥和私钥(建立与码云的连接) 三.在码云上新建项目(建议在组织的基础上)   四.在码 ...

  8. EJB系列 - EJB高级概念

    本人博客文章网址:https://www.peretang.com/ejb-advanced-concepts/ EJB内幕 幕后的EJB:容器会为每一个bean实例自动生成称为EJB对象的代理, 由 ...

  9. H5 data-*容易忽略的问题

    H5添加了data-*属性,非常方便 但经常忽略小写的问题, H5要求属性名全部小写,驼峰式命名的习惯掉坑了 测试代码如下: <html> <head> <script ...

  10. C# 中关于接口实现、显示实现接口以及继承

    先列出我写的代码: 接口以及抽象类.实现类 public interface IA { void H(); } public interface IB { void H(); } public abs ...