zoukankan      html  css  js  c++  java
  • Palindrome Partitioning II

    Palindrome Partitioning II

    问题:

    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

    思路:

      动态规划

    我的代码1:(思路和代码都是正确的,但是对于大数据超时啊) http://www.lintcode.com/zh-cn/problem/palindrome-partitioning-ii/ 都通过不了啊

    public class Solution {
        public int minCut(String s) {
            if(s==null || s.length()==0 || isPalindrome(s))    return 0;
            int len = s.length();
            int[] record = new int[len];
            
            for(int i=0; i<len; i++)
            {
                int min = Integer.MAX_VALUE;
                for(int j=0; j<=i; j++)
                {
                    String sub = s.substring(j,i+1);
                    if(isPalindrome(sub))
                    {
                        min = Math.min(min, (j==0?0:1+record[j-1]));
                    }
                }
                record[i] = min;
            }
            return record[len-1];
            
        }
        public boolean isPalindrome(String part)
        {
            int len = part.length();
            for(int i = 0; i < len/2; i++)
            {
                char first = part.charAt(i);
                char last = part.charAt(len - 1 - i);
                if(first != last)   return false;
            }
            return true;
        }
    }
    View Code

     我的代码2:用另外一个数组记录已经成功的substring http://www.lintcode.com/zh-cn/problem/palindrome-partitioning-ii/ 可以通过了

    public class Solution {
        public int minCut(String s) {
            if(s==null || s.length()==0 || isPalindrome(s))    return 0;
            int len = s.length();
            int[] record = new int[len];
            boolean[][] isValid = new boolean[len][len];
            
            for(int i=0; i<len; i++)
            {
                int min = Integer.MAX_VALUE;
                for(int j=0; j<=i; j++)
                {
                    String sub = s.substring(j,i+1);
                    if(isValid[j][i])
                    {
                        min = Math.min(min, (j==0?0:1+record[j-1]));
                        continue;
                    }
                    if(isPalindrome(sub))
                    {
                        min = Math.min(min, (j==0?0:1+record[j-1]));
                        isValid[j][i] = true;
                    }
                }
                record[i] = min;
            }
            return record[len-1];
            
        }
        public boolean isPalindrome(String part)
        {
            int len = part.length();
            for(int i = 0; i < len/2; i++)
            {
                char first = part.charAt(i);
                char last = part.charAt(len - 1 - i);
                if(first != last)   return false;
            }
            return true;
        }
    }
    View Code
  • 相关阅读:
    JavaScript与C# Windows应用程序交互方法
    CREATE TABLE 表名 AS SELECT 语句
    从新开始
    window下安装redis
    最终,我们都变成了机器
    这个网址很学习
    改变你一生命运的话语 不得不信
    看《超级演说家》有感
    网页布局的一点感触
    最近状态不好
  • 原文地址:https://www.cnblogs.com/sunshisonghit/p/4494076.html
Copyright © 2011-2022 走看看