LeetCode: Palindrome Partitioning II 解题报告
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.

SOLUTION 1:
使用DP来解决:
1. D[i] 表示前i 个字符切为回文需要的切割数
2. P[i][j]: S.sub(i-j) is a palindrome.
3. 递推公式: D[i] = Math.min(D[i], D[j] + 1), 0 <= j <= i - 1) ,并且要判断 P[j][i - 1]是不是回文。
4. 注意D[0] = -1的用意,它是指当整个字符串判断出是回文是,因为会D[0] + 1 其实应该是结果为0(没有任何切割),所以,应把D[0] 设置为-1
有个转移函数之后,一个问题出现了,就是如何判断[i,j]是否是回文?每次都从i到j比较一遍?太浪费了,这里也是一个DP问题。
定义函数
P[i][j] = true if [i,j]为回文
那么
P[i][j] = str[i] == str[j] && P[i+1][j-1];
public class Solution {
public int minCut(String s) {
if (s == null || s.length() == 0) {
return 0;
}
int len = s.length();
// D[i]: 前i 个字符切为回文需要的切割数
int[] D = new int[len + 1];
D[0] = -1;
// P[i][j]: S.sub(i-j) is a palindrome.
boolean[][] P = new boolean[len][len];
for (int i = 1; i <= len; i++) {
// The worst case is cut character one by one.
D[i] = i - 1;
for (int j = 0; j <= i - 1; j++) {
P[j][i - 1] = false;
if (s.charAt(j) == s.charAt(i - 1) && (i - 1 - j <= 2 || P[j + 1][i - 2])) {
P[j][i - 1] = true;
D[i] = Math.min(D[i], D[j] + 1);
}
}
}
return D[len];
}
}
GITHUB:
https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/dp/MinCut_1206.java
LeetCode: Palindrome Partitioning II 解题报告的更多相关文章
- [LeetCode] Palindrome Partitioning II 解题笔记
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- 【LeetCode】Permutations II 解题报告
[题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...
- LeetCode: Unique Paths II 解题报告
Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution Fol ...
- [leetcode]Palindrome Partitioning II @ Python
原题地址:https://oj.leetcode.com/problems/palindrome-partitioning-ii/ 题意: Given a string s, partition s ...
- [LeetCode] Palindrome Partitioning II 拆分回文串之二
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- Leetcode: Palindrome Partitioning II
参考:http://www.cppblog.com/wicbnu/archive/2013/03/18/198565.html 我太喜欢用dfs和回溯法了,但是这些暴力的方法加上剪枝之后复杂度依然是很 ...
- LeetCode: Jump Game II 解题报告
Jump Game II Given an array of non-negative integers, you are initially positioned at the first inde ...
- LeetCode: Spiral Matrix II 解题报告-三种方法解决旋转矩阵问题
Spiral Matrix IIGiven an integer n, generate a square matrix filled with elements from 1 to n2 in sp ...
- LeetCode: Word Break II 解题报告
Word Break II Given a string s and a dictionary of words dict, add spaces in s to construct a senten ...
随机推荐
- 【Android】详解Android的menu菜单
在软件应用过程中,菜单的存在是必不可少的,我这次讲一下,我对android菜单的一个基础做法吧 Android的菜单分为三种类型:选项菜单(Option Menu).上下文菜单(Context Men ...
- 关于tensorboard启动问题
我在学习过程中遇到了tensorboard无法启动的问题. 按照网上的教程,我无法正常启动tensorboard,全过程没有报错,但是打开tensorboard显示 No dashboards are ...
- sql的行转列(PIVOT)与列转行(UNPIVOT) webapi 跨域问题 Dapper 链式查询 扩展 T4 代码生成 Demo (抽奖程序)
sql的行转列(PIVOT)与列转行(UNPIVOT) 在做数据统计的时候,行转列,列转行是经常碰到的问题.case when方式太麻烦了,而且可扩展性不强,可以使用 PIVOT,UNPIVOT比 ...
- rhel7 ifconfig command not found
同事扔过来一个rhel7.2的系统,登录后发现没有安装ifconfig命令: # ifconfig -bash: ifconfig: command not found 先看看环境变量: # echo ...
- android控件拖动,移动、解决父布局重绘时控件回到原点
这是主要代码: 保证其params发生改变,相对于父布局的位置就能达到位置移动到原来的位置 // 每次移动都要设置其layout,不然由于父布局可能嵌套listview,当父布局发生改变冲毁(如下拉刷 ...
- ldd 的一个安全问题
我们知道“ldd”这个命令主要是被程序员或是管理员用来查看可执行文件所依赖的动态链接库的.是的,这就是这个命令的用处.可是,这个命令比你想像的要危险得多,也许很多黑客通过ldd的安全问题来攻击你的服务 ...
- mysql 大数据提取
今天要重五百多万的一个数据库表 提取 大约五十万条数据,刚开始的解决思路是: 先把数据查询出来,然后再导出来,然后再设计一个数据库表格,把这些数据导入,最后导出数据和导入数据花费了很多时间,最后向同事 ...
- SQL Server中判断字符串出现的位置及字符串截取
首先建一张测试表: )); insert into teststring values ('张三,李四,王五,马六,萧十一,皇宫'); 1.判断字符串中某字符(字符串)出现的次数,第一次出现的位置最后 ...
- Django form入门详解--2
调整form的输出格式: 默认情况下form的格式化输出是基本table的样式的.但是django中还是为form提供发别的输出样式 1.默认的table样式输出 <html> <h ...
- ACM会议列表与介绍(2014/05/06)
Conferences ACM SEACM Southeast Regional Conference ACM Southeast Regional Conference the oldest, co ...