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 解题报告的更多相关文章

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

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

  2. 【LeetCode】Permutations II 解题报告

    [题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...

  3. LeetCode: Unique Paths II 解题报告

    Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution  Fol ...

  4. [leetcode]Palindrome Partitioning II @ Python

    原题地址:https://oj.leetcode.com/problems/palindrome-partitioning-ii/ 题意: Given a string s, partition s  ...

  5. [LeetCode] Palindrome Partitioning II 拆分回文串之二

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

  6. Leetcode: Palindrome Partitioning II

    参考:http://www.cppblog.com/wicbnu/archive/2013/03/18/198565.html 我太喜欢用dfs和回溯法了,但是这些暴力的方法加上剪枝之后复杂度依然是很 ...

  7. LeetCode: Jump Game II 解题报告

    Jump Game II Given an array of non-negative integers, you are initially positioned at the first inde ...

  8. LeetCode: Spiral Matrix II 解题报告-三种方法解决旋转矩阵问题

    Spiral Matrix IIGiven an integer n, generate a square matrix filled with elements from 1 to n2 in sp ...

  9. 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 ...

随机推荐

  1. 老男孩linux实战培训初级班第三次课课前考试题

    ################################################################ 本文内容摘录于老男孩linux实战运维培训中心考试题 如有转载,请务必 ...

  2. mysql/mariadb基于ssl的主从复制

    当mysql/mariadb跨越互联网进行复制时别人可以窃取到mysql/mariadb的复制信息, 这些信息是明文的, 因此存在不安全性, 这里通过ssl对复制的信息进行加密 1. 创建证书中心 在 ...

  3. Linux基本数据类型大小——int,char,long int,long long int

    转自:http://paddy-w.iteye.com/blog/1403217 在Linux操作系统下使用GCC进行编程,目前一般的处理器为32位字宽,下面是/usr/include/limit.h ...

  4. C/C++函数指针(typedef简化定义)

    学习要点:        1,函数地址的一般定义和typedef简化定义;        2,函数地址的获取;        3,A函数地址作为B函数参数的传递;    函数存放在内存的代码区域内,它 ...

  5. 【Java】解析Java对XML的操作

    目录结构: contents structure [+] 什么是XML 解析XML 使用DOM解析 使用SAX解析 使用PULL解析 使用dom4j解析xml dom4j的部分API 打印一个XML文 ...

  6. Spring3整合Quartz实现定时作业

    Quartz是OpenSymphony开源组织在Job scheduling领域又一个开源项目,它可以与J2EE与J2SE应用程序相结合也可以单独使用.Quartz可以用来创建简单或为运行十个,百个, ...

  7. C# 编写Windows Service(windows服务程序)

    C# 编写Windows Service(windows服务程序)   Windows Service简介: 一个Windows服务程序是在Windows操作系统下能完成特定功能的可执行的应用程序.W ...

  8. objgraph无法生成分析图的原因

    x = [1, 2, 3]y = [x, dict(key1=x)]z = [y, (x, y)] import objgraphobjgraph.show_refs([y],filename='sa ...

  9. (麻省理工免费课程)C语言内存管理和C++面向对象编程

    此课程有全部讲义和习题. 课程描述实在得令人发指.翻译如下: 您是否由于自己的Python程序比同僚们的C程序慢而垂头丧气?你是否想不用JAVA实现面向对象?加入我们,学习C和C++吧!我们带您从简单 ...

  10. selenium实战脚本集(2)——简单的知乎爬虫

    背景 很多同学在工作中是没有selenium的实战环境的,因此自学的同学会感到有力无处使,想学习但又不知道怎么练习.其实学习新东西的道理都是想通的,那就是反复练习.这里乙醇会给出一些有用的,也富有挑战 ...