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. ThinkPHP中的内置标签

    ThinkPHP中的内置标签 1.内置标签分类 闭合标签 <tag></tag> 开放标签 <tag /> 2.包含文件标签 主要功能:实现对文件的包含(类似于re ...

  2. keep alive的相关介绍

        无论Windows还是linux,Keepalive就三个配置参数.下文以linux环境为介绍. Technorati 标签: keepalive     tcp_sock结构体中有三个有关的 ...

  3. Asp.net 生成验证码

    生成验证码一般来说大体有这么几步: 1.生成验证码字符串,一般由四个或更多随机字符拼凑而成: 2.填充图片背景,并绘制图片的背景噪音线: 3.将验证码绘制到图片中: 4.绘制前景噪点: 5.返回图片流 ...

  4. 10.NFS V4.2

    这里只演示使用keytab,也就是客户端与服务端进行keberos进行安全验证连接(注意时间服务器要同步!如果不同步,Kerberos无法通过验证) server端:192.168.1.109 1.y ...

  5. SQL 复杂查询

    一.子查询 .相关子查询 相关子查询是指需要引用主查询列表的子查询语句,相关子查询是通过EXISTS谓词来实现的.下面以显示工作在"new york"的所有雇员为例,说明相关子查询 ...

  6. JS对select动态添加options操作[IE&FireFox兼容]

    <select id="ddlResourceType" onchange="getvalue(this)"> </select> 动态 ...

  7. Win7在CMD命令行中使用管理员权限运行命令

    使用命令: runas /user:administrator 需要执行的命令 如下:

  8. 8款实用的Jquery瀑布流插件

    1.网友Null分享Jquery响应式瀑布流布局插件 首先非常感谢网友Null的无私分享,此作品是一款响应式瀑布流布局Jquery插件,网友Null增加了一个屏幕自适应和响应式,响应式就是支持智能手机 ...

  9. CentOS 6.4 升级 Mysq5.5l方法 和 用户远程登录数据库

    一:.在这里我们都知道 系统的yum源Mysql版本一般都是5.1 5.2的比较多 但是有些程序 必须要5.5以上的版本才能支持 这时候我们应该怎么办呢  编译安装也太慢 太费时间  那么我们就必要要 ...

  10. Error_code: 2003

    DB:5.6.16 配置:主从 MySQL主从断掉,从库警告日志出现大量的Error_code: 2003Slave I/O error connecting to master .......ret ...