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的更多相关文章

  1. 19. Palindrome Partitioning && Palindrome Partitioning II (回文分割)

    Palindrome Partitioning Given a string s, partition s such that every substring of the partition is ...

  2. LeetCode:Palindrome Partitioning,Palindrome Partitioning II

    LeetCode:Palindrome Partitioning 题目如下:(把一个字符串划分成几个回文子串,枚举所有可能的划分) Given a string s, partition s such ...

  3. 【leetcode】Palindrome Partitioning II

    Palindrome Partitioning II Given a string s, partition s such that every substring of the partition ...

  4. leetcode@ [131/132] Palindrome Partitioning & Palindrome Partitioning II

    https://leetcode.com/problems/palindrome-partitioning/ Given a string s, partition s such that every ...

  5. [LeetCode] Palindrome Partitioning II 解题笔记

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

  6. 动态规划——Palindrome Partitioning II

    Palindrome Partitioning II 这个题意思挺好理解,提供一个字符串s,将s分割成多个子串,这些字串都是回文,要求输出分割的最小次数. Example:Input: "a ...

  7. leetcode 131. Palindrome Partitioning 、132. Palindrome Partitioning II

    131. Palindrome Partitioning substr使用的是坐标值,不使用.begin()..end()这种迭代器 使用dfs,类似于subsets的题,每次判断要不要加入这个数 s ...

  8. LeetCode: Palindrome Partitioning II 解题报告

    Palindrome Partitioning II Given a string s, partition s such that every substring of the partition ...

  9. 【LeetCode】132. Palindrome Partitioning II

    Palindrome Partitioning II  Given a string s, partition s such that every substring of the partition ...

  10. leetcode132. Palindrome Partitioning II

    leetcode132. Palindrome Partitioning II 题意: 给定一个字符串s,分区使分区的每个子字符串都是回文. 返回对于s的回文分割所需的最小削减. 例如,给定s =&q ...

随机推荐

  1. web前端炫酷实用的HTML5应用和jQuery插件

    又开始了新的一天,我们也将继续为大家分享许多优秀的HTML5应用和jQuery插件,作为前端开发者来说,这些资源可以帮助你在项目开发上派上用场.下面一起来看看这些炫酷而实用的HTML5应用和jQuer ...

  2. Codevs 2898 卢斯的进位制

    时间限制: 1 s  空间限制: 32000 KB  题目等级 : 青铜 Bronze 题目描述 Description 著名科学家卢斯为了检查学生对进位制的理解,他给出了如下的一张加法表,表中的字母 ...

  3. C++ 实现不能被继承的类

    方法一: #include <iostream> using namespace std; class A { public: static A* getInstance(); stati ...

  4. xmlspy注册后打开报错的解决办法

    XMLSpy 2011中文版破解补丁使用方法 1.如果你下载的版本是r2sp1的话(r2不用此步骤),先用补丁主程序(altova.xmlspy.v2011r2sp1b-patch.exe).2.XM ...

  5. WPF中XAML转义字符

    字符 转义字符 备注 & (ampersand) & 这个没什么特别的,几乎所有的地方都需要使用转义字符 > (greater-than character) > 在属性( ...

  6. WinForm 实现验证码

    private void CheckIdentifyingCode() { Random r = new Random(); string str = ""; for (int i ...

  7. jdbc 连接 mysql 获取 数据集 条数

    package nona; import java.io.IOException; import java.io.PrintWriter; import java.sql.Connection; im ...

  8. How to running Job from a Form

    For Example we wanna run a Job with name  "FAN_TableList_CSV". So you must create a button ...

  9. WPF编译时提示“...不包含适合于入口点的静态‘Main’方法 ...”

    今天看了一下wpf的Application类方面的知识,一个windows应用程序由一个Application类的实例表示,该类跟踪在应用程序中打开的所有窗口,决定何时关闭应用程序(属性 Shutdo ...

  10. float类型进行计算精度丢失的问题

    今天一个案子,用户反映数量差异明明是 2.0-1.8,显示的结果却为0.20000005,就自己写了段方法测试了一下:package test1;public class Test2 {/*** @p ...