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 ...
随机推荐
- Magento EAV模型
网址:http://www.ruiwant.com/magento-for-dev-part-7-advanced-orm-entity-attribute-value.html
- Linux安装最新版Mono,Jexus(截至2015年12月30日)
安装系统必备: yum -y install gcc gcc-c++ bison pkgconfig glib2-devel gettext make libpng-devel libjpeg-dev ...
- iOS AFNetWorking下得Basic Auth认证请求方式
我新入职了一家公司,做了一个项目,服务器的大哥说他采用的是Basic Auth认证请求方式,一般我们用的都是OAuth的认证方式,下面我们就对比一下这两种认证方式 百度百科得到如下 Basic Aut ...
- git detached
git提交的时候,本地已经提交,却怎么也推送不到服务器,也没显示错误,只显示 everything-up-to-date : 原因是git不在master分支,而是处于detached head(匿名 ...
- ural 1091. Tmutarakan Exams(容斥)
http://acm.timus.ru/problem.aspx? space=1&num=1091 从1~s中选出k个数,使得k个数的最大公约数大于1,问这种取法有多少种. (2<=k ...
- LIGHT OJ 1199 - Partitioning Game
传送门 1199 - Partitioning Game PDF (English) problem=1199" style="color:rgb(79,107,114)&q ...
- java设置配置session过期时间的方法
1) Timeout in the deployment descriptor (web.xml)以分钟为单位 代码如下 复制代码 <web-app ...> <session-co ...
- 数据库事务隔离级别+Spring 声明性事务隔离级别
数据库事务隔离级别 数据库提供了四种事务隔离级别, 不同的隔离级别采用不同的锁类开来实现. 在四种隔离级别中, Serializable的级别最高, Read Uncommited级别最低. 大多数数 ...
- MySQL与Oracle的区别之我见
1. 大的方面(宏观) Oracle为商用数据库,行业中占据相当的地位:市场占比2012年为40%.开发.管理资源相当丰富,有自己的metalink,我也曾用过,有什么问题,都能在那里得到较快速度的解 ...
- MySQL 5.6学习笔记(查询数据、插入、更新、删除数据)
1. 查询语法 SELECT {*|<字段列表>} [FROM table_references [WHERE where_condition] [GROUP BY {col_name | ...