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的二维数组记录是否是回文串。
 
  1. public class Solution {
  2. public int minCut(String s) {
  3.  
  4. int len = s.length();
  5. if( len <= 1)
  6. return 0;
  7. char[] word = s.toCharArray();
  8. int[] dp = new int[len];
  9. boolean[][] isPalindrome = new boolean[len][len];
  10. for( int i = 0;i<len;i++){
  11. int min = i;
  12. for( int j = 0;j<=i;j++){
  13. if( word[i] == word[j] && ( j+1>=i-1 || isPalindrome[j+1][i-1] )){
  14. isPalindrome[j][i] = true;
  15. min = j==0?0:Math.min(min,dp[j-1]+1);
  16. }
  17. }
  18. dp[i] = min;
  19. }
  20.  
  21. return dp[len-1];
  22. }
  23.  
  24. }

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. Java 时间、日期类

    1. System类 currentTimeMillis():返回当前时间的long型值.此long值是从1970年1月1日0点0分00秒开始到当前的毫秒数. 此方法常用来计算时间差. 2. Date ...

  2. Java 枚举&注解

    枚举类 如何自定义枚举类 JDK1.5之前需要自定义枚举类 JDK 1.5 新增的 enum 关键字用于定义枚举类 若枚举只有一个成员, 则可以作为一种单例模式的实现方式 //枚举类 class Se ...

  3. 多线程同步内功心法——PV操作上(未完待续。。。)

    阅读本篇之前推荐阅读以下姊妹篇: <秒杀多线程第四篇一个经典的多线程同步问题> <秒杀多线程第五篇经典线程同步关键段CS> <秒杀多线程第六篇经典线程同步事件Event& ...

  4. Android之sdcard操作

    private static final String FILENAME = "abc.txt"; private static final String DIR = " ...

  5. Google search

    filetype: active directory filetype: eg : lady gaga filetype:mp3 link: eg : link : supinfo.com(链接到su ...

  6. 实现IOS圆角风格的列表ListView

    这段代码目前已经加在我的一个jar包androidkit中,还没发布. 适用于android1.6以上,不依赖其他jar包 使用时不需要继承这里的RoundListAdapter.只需要在你实现了Li ...

  7. SharePoint 2013 搜索体系结构

    博客地址:http://blog.csdn.net/FoxDave 本文参考自微软官方的Chart,记录一下,算是自己对这部分知识的总结. Microsoft® SharePoint® Server ...

  8. python解无忧公主的数学时间097.py

    python解无忧公主的数学时间097.py """ python解无忧公主的数学时间097.py codegay 2016年3月30日 00:17:26 http:// ...

  9. yeild

    正在使用cpu的线程,退出,返回等待池,其他优先级相同的线程竞争上岗.

  10. HTML--10Jquery

    在<网页制作Dreamweaver(悬浮动态分层导航)>中,运用到了jQuery的技术,轻松实现了菜单的下拉.显示.隐藏的效果,不必再用样式表一点点地修改,省去了很多麻烦,那么jQuery ...