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. StompClient的包装类

    为了简化MQ调用,写了个StompClient的包装类,可以供需要的参考: unit FStompClient; interface uses SysUtils, Windows, Messages, ...

  2. 集成支付宝钱包支付 iOS SDK 的方法与经验

    下载 首先,你要想找到这个SDK,都得费点功夫.现在的SDK改名叫移动支付集成开发包了,下载页面在 这里 (http://t.cn/8ksiklD)的 “请点此下载集成开发包(http://t.cn/ ...

  3. Install MySQL 5.7 on Fedora 25/24, CentOS/RHEL 7.3/6.8/5.11

    MySQL is a relational database management system (RDBMS) that runs as a server providing multi-user ...

  4. 进阶之路(基础篇) - 001 亮一个led灯

    /********************************* 代码功能:点亮一个led灯 使用函数: pinMode(引脚号,模式); digitalWrite(引脚号,电平状态); //默认 ...

  5. HDU 1710 Binary Tree Traversals (二叉树遍历)

    Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  6. C#编写的服务程序启动服务时报错误1053

    Application.StartupPath 已知解决方案一:        读写文件路径的问题 用 $"{Application.StartupPath}\\config.txt&quo ...

  7. 【转】Kotlin 和 Checked Exception

    Kotlin 和 Checked Exception 最近 JetBrains 的 Kotlin 语言忽然成了热门话题.国内小编们传言说,Kotlin 取代了 Java,成为了 Android 的“钦 ...

  8. nodejs的Express框架源码分析、工作流程分析

    nodejs的Express框架源码分析.工作流程分析 1.Express的编写流程 2.Express关键api的使用及其作用分析 app.use(middleware); connect pack ...

  9. Android编译系统环境初始化过程分析

    Android源代码在编译之前,要先对编译环境进行初始化,其中最主要就是指定编译的类型和目标设备的型号.Android的编译类型主要有eng.userdebug和user三种,而支持的目标设备型号则是 ...

  10. 转 HystrixDashboard服务监控、Turbine聚合监控

    SpringCloud系列七:Hystrix 熔断机制(Hystrix基本配置.服务降级.HystrixDashboard服务监控.Turbine聚合监控) 1.概念:Hystrix 熔断机制 2.具 ...