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. Android计算器开发实例

    Android简单计算器开发实例如图: ==================================================== activity_main.xml 代码如下: < ...

  2. c++模板类

    c++模板类 理解编译器的编译模板过程 如何组织编写模板程序 前言常遇到询问使用模板到底是否容易的问题,我的回答是:“模板的使用是容易的,但组织编写却不容易”.看看我们几乎每天都能遇到的模板类吧,如S ...

  3. Javascript的匿名函数与自执行

    1.匿名函数 函数是JavaScript中最灵活的一种对象,这里只是讲解其匿名函数的用途.匿名函数:就是没有函数名的函数. 1.1 函数的定义,首先简单介绍一下函数的定义,大致可分为三种方式 第一种: ...

  4. PHP站内搜索、多关键字、加亮显示

    php搜索代码: 搜索以PHP100开头: SELECT * FROM teble WHERE title  LIKE  'PHP100%' 搜索以PHP100结束: SELECT * FROM te ...

  5. setTimeout 导致的浏览器假死

    问题   前几天,同事遇到一个浏览器假死的问题.就是浏览器在响应一个请求的时候,就突然不响应时间,进入假死状态,Cup也飙升到100%. 但是这个问题只出现在IE浏览器,chrome和Firefox等 ...

  6. OWIN support for the Web API 2 and MVC 5 integrations in Autofac

    Currently, in the both the Web API and MVC frameworks, dependency injection support does not come in ...

  7. Senparc.Weixin.MP.Sample 配置redis服务器密码

    redis.windows-service.conf中加  requirepass 你的密码 <!-- Cache.Redis连接配置 --> <add key="Cach ...

  8. HTTP错误404.13 - Not Found 请求筛选模块被配置为拒绝超过请求内容长度的请求

    http://www.cnblogs.com/JKqingxinfeng/archive/2012/10/29/2744663.html HTTP错误404.13 - Not Found 请求筛选模块 ...

  9. thinkphp 3.2响应头 x-powered-by 修改

    起初是看到千图网的登录链接 查看到的 自己做的网站也看了下 修改的办法就是TP3.2.2 的框架里 具体路径是D:\www\ThinkPHP\Library\Think\View.class.php ...

  10. Python 之匿名函数和偏函数

    匿名函数与偏函数 匿名函数 Python允许使用lambda关键字创造匿名函数,lambda表达式用于定义匿名函数,它返回可调用的函数对象,语法如下: lambda arg1, arg2, … : e ...