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.

解题思路:

因为是Hard,用上题的结果计算肯定是超时的,本题需要用dp的思路,开一个boolean[][]的数组计算i-j是否为palindrome,递推关系为s.charAt(j) == s.charAt(i) &&  isPal[j + 1][i - 1]) → isPal[j][i] = true,同时dp[i] = Math.min(dp[i], dp[j - 1] + 1),JAVA实现如下:

    public int minCut(String s) {
int[] dp = new int[s.length()];
for (int i = 0; i < dp.length; i++)
dp[i] = i;
boolean isPal[][] = new boolean[s.length()][s.length()];
for (int i = 1; i < s.length(); i++)
for (int j = i; j >= 0; j--)
if (s.charAt(j) == s.charAt(i)
&& (j + 1 >= i - 1 || isPal[j + 1][i - 1])) {
isPal[j][i] = true;
dp[i] = j == 0 ? 0 : Math.min(dp[i], dp[j - 1] + 1);
}
return dp[dp.length - 1];
}

Java for LeetCode 132 Palindrome Partitioning II的更多相关文章

  1. leetcode 132. Palindrome Partitioning II ----- java

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

  2. Leetcode 132. Palindrome Partitioning II

    求次数的问题一般用DP class Solution(object): def minCut(self, s): """ :type s: str :rtype: int ...

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

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

  4. 【LeetCode】132. Palindrome Partitioning II

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

  5. 【leetcode】Palindrome Partitioning II

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

  6. 【leetcode】Palindrome Partitioning II(hard) ☆

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

  7. 132. Palindrome Partitioning II

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

  8. Java for LeetCode 131 Palindrome Partitioning

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

  9. [LeetCode] 132. Palindrome Partitioning II_ Hard tag: Dynamic Programming

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

随机推荐

  1. Android Touch事件传递机制详解 上

    最近总是遇到关于Android Touch事件的问题,如:滑动冲突的问题,以前也花时间学习过Android Touch事件的传递机制,可以每次用起来的时候总是忘记了,索性自己总结一下写篇文章避免以后忘 ...

  2. 【hibernate】hibernate不同版本的命名策略

    ===================================================hibernate 4命名策略如下================================ ...

  3. 百度 api 测试 & python

    ''' 一.文字转语音api,树莓派天气闹钟爬取实时天气数据转换为语音,设置树莓派计划任务 ''' from aip import AipSpeech import requests import r ...

  4. Solidworks如何使用Toolbox

    Toolbox不仅仅是智能扣件.事实上,一般常见的轴承,螺栓,齿轮都有了,点击右侧的设计库即可展开Toolbox   配置完成后我只留下一个GB   比如我要选一个圆锥滚子轴承,从右边拖进来即可   ...

  5. Shell脚本值:运算符

    算术运算符 原生bash不支持简单的数学运算,但是可以通过其他命令来实现,例如 awk 和 expr,expr 最常用. expr 是一款表达式计算工具,使用它能完成表达式的求值操作. 例如:实现两个 ...

  6. C#命名空间大全详细教程

    www.51rgb.com System 命名空间包含了定义数据类型.事件和事件处理程序等基本类: System.Data 命名空间包含了提供数据访问功能的命名空间和类: System.IO 命名空间 ...

  7. 转:DDR中端接技术基本概念

    DDR中端接技术基本概念  版权声明:转载请注明出处:http://blog.csdn.net/lg2lh https://blog.csdn.net/lg2lh/article/details/90 ...

  8. python windows package/module 安装

    方法一: cmd中运行pip install xx 注意:1.pip添加至环境变量 2.系统自带这些安装模块 运行pip freeze > requirements.txt 可以在require ...

  9. scrapy之Logging使用

    #coding:utf-8 __author__ = 'similarface' ###################### ##Logging的使用 ###################### ...

  10. 你要的最后一个字符就在下面这个字符串里,这个字符是下面整个字符串中第一个只出现一次的字符。(比如,串是abaccdeff,那么正确字符就是b了)

    include "stdafx.h" #include<iostream> #include<string> using namespace std; in ...