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, 一个是对那一部分是palindrome的二维dp。 还有一个是 对cut个数的一维dp。
public class Solution {
int[][] map = null;
public int minCut(String s) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
map = new int[s.length()][s.length()];
int[] cut = new int[s.length()];
for(int i = 0; i < s.length(); i++)
map[i][i] = 1;
for(int i = 0; i < s.length() - 1; i ++){
if(s.charAt(i) == s.charAt(i + 1)) map[i][i + 1] = 1;
else map[i][i + 1] = -1;
}
for(int i = 0; i < s.length(); i ++){
cut[i] = i;
for(int j = i; j < s.length(); j ++){
map[i][j] = checkPartition(s, i, j);
}
}
for(int j = 0; j < s.length(); j ++){
for(int i = 0; i < j; i ++){
if(map[i][j] == 1) cut[j] = Math.min(cut[j], 1 + cut[i]);
}
}
return cut[s.length() - 1];
}
public int checkPartition(String s, int start, int end){
if(map[start][end] != 0) return map[start][end];
if(s.charAt(start) != s.charAt(end)) return -1;
return checkPartition(s, start + 1, end - 1);
}
}
public class Solution {
public int minCut(String s) {
int leng = s.length();
if(leng == 0 || leng == 1) return 0;
boolean[][] isPal = new boolean[leng][leng]; int[] dp = new int[leng];
for (int i = 0; i < leng; i++) {
dp[i] = leng - 1 - i;
} for (int i = leng - 1; i >= 0; --i) {
for (int j = i; j < leng; ++j) {
if (s.charAt(i) == s.charAt(j) && (j <= i + 2 || (i + 1 < leng && j - 1 >= 0 && isPal[i + 1][j - 1]))) {
isPal[i][j] = true;
if(j+1 < leng){
dp[i] = Math.min(dp[i], 1 + dp[j + 1]);
}else {
dp[i] = 0;
}
}
}
} return dp[0];
}
}
Palindrome Partitioning II的更多相关文章
- 19. Palindrome Partitioning && Palindrome Partitioning II (回文分割)
Palindrome Partitioning Given a string s, partition s such that every substring of the partition is ...
- LeetCode:Palindrome Partitioning,Palindrome Partitioning II
LeetCode:Palindrome Partitioning 题目如下:(把一个字符串划分成几个回文子串,枚举所有可能的划分) Given a string s, partition s such ...
- 【leetcode】Palindrome Partitioning II
Palindrome Partitioning II Given a string s, partition s such that every substring of the partition ...
- leetcode@ [131/132] Palindrome Partitioning & Palindrome Partitioning II
https://leetcode.com/problems/palindrome-partitioning/ Given a string s, partition s such that every ...
- [LeetCode] Palindrome Partitioning II 解题笔记
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- 动态规划——Palindrome Partitioning II
Palindrome Partitioning II 这个题意思挺好理解,提供一个字符串s,将s分割成多个子串,这些字串都是回文,要求输出分割的最小次数. Example:Input: "a ...
- leetcode 131. Palindrome Partitioning 、132. Palindrome Partitioning II
131. Palindrome Partitioning substr使用的是坐标值,不使用.begin()..end()这种迭代器 使用dfs,类似于subsets的题,每次判断要不要加入这个数 s ...
- LeetCode: Palindrome Partitioning II 解题报告
Palindrome Partitioning II Given a string s, partition s such that every substring of the partition ...
- 【LeetCode】132. Palindrome Partitioning II
Palindrome Partitioning II Given a string s, partition s such that every substring of the partition ...
- leetcode132. Palindrome Partitioning II
leetcode132. Palindrome Partitioning II 题意: 给定一个字符串s,分区使分区的每个子字符串都是回文. 返回对于s的回文分割所需的最小削减. 例如,给定s =&q ...
随机推荐
- Install GDAL in OpenSUSE 12.3 Linux
Runtime Enviroment:Open SUSE Linux *i385 Notice:if any command disavliable ,you can copy the paramet ...
- PSP编程初探 Hello World
自己有一台PSP2000,玩了这么长时间的游戏,所以打算去探究一下PSP这个平台的程序的构建方式. 在网上搜了很多资料,感觉能用上的不多,毕竟这太小众了,通过自己的探索,总结了一下. 先搭建MinGW ...
- 自动化测试平台CATP
CATP:报文类工具,可以测试功能
- CString使用
1. 空间分配,如果不是它自己的空间分配方式,需要用函数来手动分配空间,否则大家指向同一块地址,取得内容一样 例子,读取文件到CString ,没有给CString 对象分配空间,而且不是他定义的开拓 ...
- fluent nhibernate 初体验
离开.net框架两年时间,发展的很快呀.原先自我感觉良好到以为只差一个MVP的考核什么的,现在觉得真的差好远了. 呵呵,废话就不多说了.这次花了两天时间才拿下fluent nhibernate的fir ...
- mysql mmm高可用架构设计
项目概述:搭建主从,双主,安装Perl模块 安装配置mmm软件 测试 硬件环境:4台虚拟PC 软件环境:rehl6.5 MySQL-5.6.26 percona-xtrabackup-2.3.4 ...
- 3.servlet实现页面的跳转
效果: 在网页的输入框中输入lily,跳到成功的页面(请求转发),输入的不是lily则跳到百度的页面(跳到工程之外的页面,则是请求重定向) 1.建Web project“2Servlet_Basic” ...
- WP开发笔记——WP7 SDK使用技巧
俗话说的好,工欲善其事,必先利其器. 入门WP开发之前,免不了要先接触开发环境和开发工具.使用WP7 SDK进行开发,我们需要掌握SDK的一些实用技巧,以便我们的开发. 一.开启/关闭电脑键盘输入 W ...
- centos5.4下mysql主从复制
centos5.4下mysql主从复制配置分享. 本文转自:http://www.jbxue.com/article/771.html 安装环境:centos 5.4 mysql版本:mysql 5. ...
- Android如何设置标题栏的高度
转载自: http://blog.csdn.net/yuxiaohui78/article/details/8222993 新建一个styles.xml 1 <?xmlversion=" ...