【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 ...
随机推荐
- 百度UEditor 用require 引入 Thinkphp5 ,图片上传问题
用require引入,用了10分钟:上传图片,用了一个早上(吐血一地.....) 重点:require引入成功后,在需要引用UEditor的文件开头加入(ue的文件夹路径) window.UEDITO ...
- What is the difference between application server and web server?
http://stackoverflow.com/questions/936197/what-is-the-difference-between-application-server-and-web- ...
- JavaScript 函数语法
函数就是包裹在花括号中的代码块,前面使用了关键词 function: function functionname() { 这里是要执行的代码 } 当调用该函数时,会执行函数内的代码. 可以在某事件发生 ...
- 嵌入式开发之赛灵思 xilinx Zynq芯片简介---Zynq-7000 EPP (XC7Z010 and XC7Z020)
(1)企业简介 作为DSP 和视频应用领域的头号 FPGA 供应商,赛灵思致力于通过其目标设计平台提供业内领先的 DSP 开发工具.方法.IP 和技术支持.赛灵思面向 DSP 的目标设计平台将这些元素 ...
- vivo 手机的USB调试功能
- Unity3D学习笔记——NGUI之UIToggle
UIToggle:切换,从名字就能知道这个组件能做什么.这个组件有两种状态ON/OFF.可以用于创建checkboxes, tabs, radio button groups. 效果展示: 一:使用步 ...
- Android Studio Error -- Could not create the Java Virtual Machine
:app:dexDebug Error: Could not create the Java Virtual Machine. Error: A fatal exception has occurre ...
- JavaScript Observer Pattern
var Users = { list: [], listeners: {}, add: function(name) { this.list.push({name: name}); this.disp ...
- 【BZOJ1053】[HAOI2007]反素数ant 暴力
[BZOJ1053][HAOI2007]反素数ant Description 对于任何正整数x,其约数的个数记作g(x).例如g(1)=1.g(6)=4.如果某个正整数x满足:g(x)>g(i) ...
- 160822、关于javascrip ==(等号) 和===(恒等)判断
说明 在JavaScript中,下面的值被当做假(false),除了下面列出的值,都被当做真(true): false null undefined 空字符串 数字 0 NaN //属性是代表非数字值 ...