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. “更高效率:标准化+简约风+移动化”--K2 BPM老客户交流会

    主题:工作流主数据标准化和移动工作流带来的企业沟通建设机会 嘉宾:李瑞延(盛大网络IT总监) 公司管理需要更好的工作流 -为决策提供依据 通过对各级业务公司各类流程数据的获取与分析,为管理决策提供必要 ...

  2. jquery animate()方法使用的注意事项

    当使用 animate() 时,必须使用 Camel 标记法书写所有的属性名,比如,必须使用 paddingLeft 而不是 padding-left,使用 marginRight 而不是 margi ...

  3. java基础之 泛型

    泛型(Generic type 或者generics)是对 Java 语言的类型系统的一种扩展,以支持创建可以按类型进行参数化的类.可以把类型参数看作是使用参数化类型时指定的类型的一个占位符,就像方法 ...

  4. c#读取文本文档实践1-File.ReadAllLines()

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.I ...

  5. 使用SharedPreferences进行数据存储

    使用SharedPreferences进行数据存储 很多时候我们开发的软件需要向用户提供软件参数设置功能,例如我们常用的QQ,用户可以设置是否允许陌生人添加自己为好友.对于软件配置参数的保存,如果是w ...

  6. equals() 与 hashcode() 的区别与联系

    两者都是从Object类继承的方法,Object中equals方法比较的是this和参数传进来的对象的引用地址是否相同,这样的话,equals返回值为true的必要充分条件就是两者指向同一个对象,那么 ...

  7. IOS NSInvocation用法简介

    IOS NSInvocation用法简介 2012-10-25 19:59 来源:博客园 作者:csj007523 字号:T|T [摘要]在 iOS中可以直接调用某个对象的消息方式有两种,其中一种就是 ...

  8. 聚簇(Cluster)和聚簇表(Cluster Table)

    聚簇(Cluster)和聚簇表(Cluster Table) 时间:2010-03-13 23:12来源:OralanDBA.CN 作者:AlanSawyer 点击:157次 1.创建聚簇 icmad ...

  9. (function($){...})(jQuery) 含义

    最近在项目js文件末端中发现这样的代码,对于前端技术比较薄弱的我,着实还是有点晕,好在查阅到了相关资料,现解释如下: (function($){  $.plugin = new org.plugin. ...

  10. How many instances created in the WebContainer

    When the Servlet container starts, it: reads web.xml; finds the declared Servlets in the classpath; ...