【leetcode刷题笔记】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.
题解: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的更多相关文章
- 【leetcode刷题笔记】Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- 【leetcode刷题笔记】Subsets II
Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...
- 【leetcode刷题笔记】N-Queens II
Follow up for N-Queens problem. Now, instead outputting board configurations, return the total numbe ...
- LeetCode刷题笔记和想法(C++)
主要用于记录在LeetCode刷题的过程中学习到的一些思想和自己的想法,希望通过leetcode提升自己的编程素养 :p 高效leetcode刷题小诀窍(这只是目前对我自己而言的小方法,之后会根据自己 ...
- 【leetcode刷题笔记】Palindrome Partitioning
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- LeetCode刷题笔记 - 12. 整数转罗马数字
学好算法很重要,然后要学好算法,大量的练习是必不可少的,LeetCode是我经常去的一个刷题网站,上面的题目非常详细,各个标签的题目都有,可以整体练习,本公众号后续会带大家做一做上面的算法题. 官方链 ...
- 18.9.10 LeetCode刷题笔记
本人算法还是比较菜的,因此大部分在刷基础题,高手勿喷 选择Python进行刷题,因为坑少,所以不太想用CPP: 1.买股票的最佳时期2 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. ...
- Leetcode刷题笔记(双指针)
1.何为双指针 双指针主要用来遍历数组,两个指针指向不同的元素,从而协同完成任务.我们也可以类比这个概念,推广到多个数组的多个指针. 若两个指针指向同一数组,遍历方向相同且不会相交,可以称之为滑动窗口 ...
- 【leetcode刷题笔记】Word Ladder II
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...
随机推荐
- Vim使用技巧(4) -- 命令行模式 【持续更新】
基本保存,退出,帮助 :help //帮助 :w //保存 :q //退出 :wq //保存后退出 :q! //强制不保存退出 %s/a/b/g //将当前文件的a全部替换成b /abc //正向查找 ...
- Gold Balanced Lineup - poj 3274 (hash)
这题,看到别人的解题报告做出来的,分析: 大概意思就是: 数组sum[i][j]表示从第1到第i头cow属性j的出现次数. 所以题目要求等价为: 求满足 sum[i][0]-sum[j][0]=sum ...
- plsql programming 08 字符串
一般, char 和 nchar 类型很少使用. 建议使用 varchar2 和 nvarchar2, 其中( n 开头的是国家字符集, 没有n开头的是数据库字符集 ) 一般也不怎么使用国家字符集 v ...
- Ext扩展的QQ表情选择面板
Ext扩展的QQ表情选择面板 define(function () { EmoteChooser = function(cfg){ this.width=340; this.height=112; t ...
- I.MX6 Ethernet MAC (ENET) MAC Address hacking
/********************************************************************* * I.MX6 Ethernet MAC (ENET) M ...
- 006android初级篇之jni数据类型映射
JNI是Java Native Interface的缩写,它提供了若干的API实现了Java和其他语言的通信(主要是C&C++) 使用JNI的副作用 一旦使用JNI,JAVA程序就丧失了JAV ...
- 安装ruby环境
安装ruby环境 通过 homebrew 安装 Ruby 1. 首先,须要在系统上安装 homebrew 在命令行下,运行下面命令就可以完毕 homebrew 的安装(安装过程中将提示输入当前用户的p ...
- cvsba-1.0.0/utils/test_cvsba.cpp:.......error: ‘numeric_limits’ is not a member of ‘std’... error: expected primary-expression before ‘float’....
cvsba库http://www.uco.es/investiga/grupos/ava/node/39,不知道怎么回事,记得以前编译没有错误,不知道作者是否更新了还是怎么着,新的现在有以下错误: 解 ...
- Laravel使用ORM操作数据
数据表 CREATE TABLE IF NOT EXISTS students( `id` INT AUTO_INCREMENT PRIMARY KEY, `name` VARCHAR(255) NO ...
- Web API 2中的操作结果
how ASP.NET Web API converts the return value from a controller action into an HTTP response message ...