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.

 求最小分割次数。
 
 
利用动态规划和一个boolean的二维数组记录是否是回文串。
 
public class Solution {
public int minCut(String s) { int len = s.length();
if( len <= 1)
return 0;
char[] word = s.toCharArray();
int[] dp = new int[len];
boolean[][] isPalindrome = new boolean[len][len];
for( int i = 0;i<len;i++){
int min = i;
for( int j = 0;j<=i;j++){
if( word[i] == word[j] && ( j+1>=i-1 || isPalindrome[j+1][i-1] )){
isPalindrome[j][i] = true;
min = j==0?0:Math.min(min,dp[j-1]+1);
}
}
dp[i] = min;
} return dp[len-1];
} }

leetcode 132. Palindrome Partitioning II ----- java的更多相关文章

  1. Java for LeetCode 132 Palindrome Partitioning II

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

  9. 132. Palindrome Partitioning II (String; DP)

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

随机推荐

  1. 简单探索ContentProviderOperation

    前面一片文章中用到了ContentProviderOperation,那么我们就来看看ContentProviderOperation到底是怎么工作的. 1. ContentProviderOpera ...

  2. (转载)Htmlparser Filter 简要归纳

    1 . 逻辑关系:与或非 AndFilter()           Creates a new instance of an AndFilter. AndFilter(NodeFilter[] pr ...

  3. java基础之 异常

    Throwable是所有Java程序中错误处理的父类,有两种资类:Error和Exception. Error:表示由JVM所侦测到的无法预期的错误,由于这是属于JVM层次的严重错误,导致JVM无法继 ...

  4. linux学习笔记3:linux的网络配置,rpm包,shell以及samba服务器的使用和安装

    1.linux下的shell<linux命令.编辑器和shell编程> (1)shell种类有很多,常用的有三种,在linux可以通过ls -l /bin/*sh 来显示所有已安装的she ...

  5. msf生成shellcode

    msfpayload windows/exec CMD = calc.exe EXITFUNC=thread C 在kali Linux2.0新版中msfpayload命令已删除,功能已集成到msfv ...

  6. hdu 2076

    ps:WA了三次...第一次头脑有点乱,很麻烦的分几种情况讨论,第二次发现,只要分别算出时针和分针的角度,然后一减就行,却忽略了哪个大的问题,第三次加上了绝对值,就好了..就是以后double型比较最 ...

  7. 一个简单的Dump转文本工具—Dump2Text

    每次电脑重装都得烦心,要把庞大的IDE重新配置一次,正准备安装Visual Stdio 2010,上网找镜像的时候发现,Visual Stdio 2013推出了Community版,不仅没有lite掉 ...

  8. Qemu+gdb跟踪内核源码

    1.编译安装Qemu Qemu源码下载地址:http://wiki.qemu.org/Download linux下可以直接用wget下载: wget http://wiki.qemu.org/dow ...

  9. csharp_ToJson的正确写法

    网上搜的,但有问题,已经改好...现在这个是正确的 public static string ToJson(DataTable dt, string jsonName)        {        ...

  10. codeforces 580C Kefa and Park(DFS)

    题目链接:http://codeforces.com/contest/580/problem/C #include<cstdio> #include<vector> #incl ...