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
  • 相关阅读:
    css 样式库
    css命名规则
    css选择器
    清除浏览器自带样式
    导航菜单制作
    清除浮动和样式重置快捷代码
    程序练习网站
    各种布局样式模板
    使用gulp解决外部编辑器修改Eclipse文件延迟刷新
    jQuery Validate 表单验证 — 用户注册简单应用
  • 原文地址:https://www.cnblogs.com/sunshisonghit/p/4494076.html
Copyright © 2011-2022 走看看