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. bootstrap之daterangepicker---汉化以及扩展

    一.扩展的功能 1.初始化时,会自动创建一个select标签: 2.当改变select值时,日期也会自动改变,并且会调用apply按钮的click事件 3.点击此处进行预览 4.github地址:ht ...

  2. js判断是否是ie浏览器且给出ie版本

    之前懒得写判断ie版本js,因为网上关于这方面的代码太多了,所以从网上拷贝了一个,放到项目上才发现由于时效性的问题,代码不生效.就自己写一个吧. 怎么去看浏览器的内核等信息 ---- js的全局对象w ...

  3. AppServ安装的一点小麻烦----

    好久,没装AppServ了,今天安装过程顺利,但是Apache,服务开启不了,页面也不能访问. 刚开始以为是版本下载错误了,先后换了N个版本,都不行. 在网上搜的过程中,偶然发现,一句话:64位系统, ...

  4. fetch()的用法

    发起获取资源请求的我们一般都会用AJAX技术,最近在学习微信小程序的时候,用到了fetch()方法. fetch()方法用于发起获取资源的请求.它返回一个promise,这个promise会在请求响应 ...

  5. JS 冒泡排序从学到优化

    目的:理解算法 深化算法 冒泡排序: 直接上动图好于文字 一个冒泡实例 45,67,23,88,21,6,99// 第一轮 6次// 45 67 23 88 21 6 99// 45 23 67 88 ...

  6. Web 前端代码规范

    Web 前端代码规范 最后更新时间:2017-06-25 原始文章链接:https://github.com/bxm0927/web-code-standards 此项目用于记录规范的.高可维护性的前 ...

  7. SyntaxError: Unexpected token < in JSON at position 0 错误

    当你使用AJAX时有设定dataType : 'json' 所以在接回传值的时候会以json格式来解析但回传的资料非json格式就会出现这个错误讯息

  8. php中curl远程调用获取数据

    $jump_url=$this->_post('locations'); $url=htmlspecialchars_decode($jump_url); $ch = curl_init(); ...

  9. 【LeetCode】116. Populating Next Right Pointers in Each Node

    题目: Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode ...

  10. php的laravel数据库版本管理器migration

    第一步:连接数据库 打开.env文件.配置DB_HOST DB_PORT DB_DATABASE=LARAVEL DB_USERNAME DB_PASSWORD 注意DB_DATABASE这一项需要自 ...