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 ...
随机推荐
- eclipse优化(部分)
1. 增强Eclipse(MyEclipse)输入代码提示功能 一般设置: (1). 打开Eclipse,选择打开" Window -- Preferences". (2). 在目 ...
- 解析XML文档之三:使用DOM解析
dom解析方法是将整个xml文档装载到内存当中,然后通过树形结构方式去解析的,这种方式只适合于在pc端的开发,不是很适合手机端的开发,毕竟来说手机的内存是没法跟pc相提并论的. 具体实现步骤如下: 第 ...
- .NET清除Session 的几个方法[clear/removeAll/remove/Abandon]
1.clear() 清空所有session对象的值,但保留会话 2.removeAll() 调用clear()方法 3.remove("SessionName") 删除某个 ...
- 汇编中PTR常见的几种用法
汇编中PTR的用法确实是令人比较头疼的,我特意搜集了一些PTR的应用实例,可以从例子中揣摩出规律: 1.MOV WORD PTR [DI],OFFSET BUF1 2.SUB BYTE ...
- 封装cookie
function cookie(name,value,expires){ switch(typeof value){ case 'string': //设置 var exp=''; if(expire ...
- 11g RAC R2 之Linux DNS 配置
在动手配置前,最好先了解下DNS的理论,以免犯不必要的错误.这都是被坑后的觉悟 -_-!!! Oracle 11g RAC 集群中引入了SCAN(SingleClientAccessName)的概念, ...
- IDEA操作GIT说明
公司的代码库从TFS升级到了GIT,我们的自动化测试代码就需要迁移到git上.操作如下: 1.安装GIT 安装完成后,在IDEA中配置git安装路径 2.在本地磁盘新建一个空目录,例如:D:\Wo ...
- Boost的自动链接功能
Boost是一个强大的C++第三方库,但是Boost的各种问题实在是很让人蛋疼.我搜到过一篇文章关于LuaBind使用Boost Build管理工具来管理源代码以及编译的博文,其第一句话就是Fuck ...
- 你的数据根本不够大,别老扯什么Hadoop了
本文原名"Don't use Hadoop when your data isn't that big ",出自有着多年从业经验的数据科学家Chris Stucchio,纽约大学柯 ...
- c 指针兼容性问题
指针兼容性问题: const指针不能赋值给非const指针. 非const指针可以赋值给const 指针,但前提是只是一层间接运算 Example: int *pt1; const *pt2; con ...