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.

给定一个字符串s,将s分割成一些子串,使每个子串都是回文。

比如,给出字符串s = "aab",

返回 1, 因为进行一次分割可以将字符串s分割成["aa","b"]这样两个回文子串

使用动态规划解决这道问题,我们需要初始化两个数组,
一个int数组cuts[i]纪录前i个字符最小的切割次数,
一个boolean数组dp[i][j]用于纪录从第i个位置到第j个位置是否是回文。
 
怎样用动态规划的思想判断从i到j之间的字符是否是回文呢,大致分为三种情况。
(1)如果i == j,也就是i和j是同一个位置的字符,一个字符肯定是回文。
(2)如果i和j是相邻的两个位置并且字符相等,i= j + 1,s.charAt(i) == s.charAt(j),  则是回文。
(3)如果i和j不想等也不相邻,如果dp[j + 1][i - 1]是回文,并且s.charAt(i) == s.charAt(j),  则是回文。
 
cuts[i]= i 设置最大的切割次数,前i个字符如果切割,最多切i次。
如果i到j之间是回文串,如果j大于0,说明从起始点到i之间右被切割出来一块i~j的回文,
有可能i和j是相邻的,也有可能是相隔了几个字符的,
前i个字符被切割的最小次数为 cuts[i]  和 cuts[j -1] +1的较小值,
这里的1指得是前i个字符中,从j到i这一块最少切割出来一次。
如果j =0,说明前i个字符串本身就是回文串,不需要在次切割,最小的切割次数为0
public class Solution {
public int minCut(String s) {
int n = s.length();
int[] cuts = new int[n];
boolean[][] dp = new boolean[n][n]; for (int i = 0; i < n; i++) {
cuts[i] = i;
for (int j = 0; j <= i; j++) {
if (s.charAt(i) == s.charAt(j) && (i - j <= 1 || dp[j + 1][i - 1])) {
dp[j][i] = true; if (j > 0) {
cuts[i] = Math.min(cuts[i], cuts[j - 1] + 1);
} else {
cuts[i] = 0;
}
} }
}
return cuts[n -1];
}
}

Palindrome Partitioning II Leetcode的更多相关文章

  1. Palindrome Partitioning II Leetcode java

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

  2. LeetCode:Palindrome Partitioning,Palindrome Partitioning II

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

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

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

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

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

  5. 【leetcode】Palindrome Partitioning II

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

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

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

  7. LeetCode: Palindrome Partitioning II 解题报告

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

  8. 【LeetCode】132. Palindrome Partitioning II

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

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

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

随机推荐

  1. 实现BPEL4WS演示:教程

    http://www.ibm.com/developerworks/cn/education/webservices/ws-bpelws/bpel_tutorial_cn.html 开始 什么是Bus ...

  2. java常用集合详解 contains

    java集合是对常用数据集合的封装,差不多就是数组吧,验证某个元素是否在数据集合里,最原始的方法是,用个循环,"某个元素"与数据集合中的每个元素逐个进行比较. java 对常用的一 ...

  3. linux中快速清空文件内容的几种方法

    这篇文章主要介绍了linux中快速清空文件内容的几种方法,需要的朋友可以参考下 $ : > filename $ > filename $ echo "" > f ...

  4. 常用的JS HTML DOM 事件

    HTML DOM 事件 HTML DOM 事件允许Javascript在HTML文档元素中注册不同事件处理程序. 事件通常与函数结合使用,函数不会在事件发生前被执行! (如用户点击按钮). 提示: 在 ...

  5. Only one statement is allowed per batch. A batch separator, such as 'GO', might be required between statements.

    When I added the file in VS I forgot to set Build Action = None from the file properties.

  6. border opacity

    div { border: 1px solid rgb(127, 0, 0); border: 1px solid rgba(255, 0, 0, .5); -webkit-background-cl ...

  7. 部署Redis for Windows服务

    一.环境 Redis Windows 版本:2.8.2104 二.植入Windows服务    > redis-server.exe --service-install redis.window ...

  8. QQ空间HD(2)-UIPopoverController其它使用

    DJTestViewController.m #import "DJTestViewController.h" #import "DJColorTableViewCont ...

  9. linq/EF/lambda 比较字符串日期时间大小

    在使用EF时,想要比较字符串类型的日期时,参考以下: SQL语句: 1 2 3 4 1)select * from TableName where StartTime > '2015-04-08 ...

  10. smarty string_format用法 取小数点后2位

    <{if $d.ul_pv}> <{$d.sum/$d.ul_pv|string_format:'%.2f'}> <{else}> 0.00 <{/if}&g ...