资讯专栏INFORMATION COLUMN

leetcode132. Palindrome Partitioning II

jeyhan / 575人阅读

摘要:题目要求现在有一个字符串,将分割为多个子字符串从而保证每个子字符串都是回数。我们只需要找到所有可以构成回数的并且得出最小值即可。即将字符作为,将字符所在的下标列表作为。再采用上面所说的方法,利用中间结果得出最小分割次数。

题目要求
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.

现在有一个字符串s,将s分割为多个子字符串从而保证每个子字符串都是回数。问最少需要分割多少次。

思路一:HashMap缓存

这道题目的核心思想是动态编程,假设我们已经知道[0,1],[0,2]...[0,i-1]每个子字符串的最少分割数,那么我们可以得出[0,i]子字符串的最小分割数。我们只需要从第i个字符开始往回找所有可以和第i个字符构成回数,假设从第j个字符到第i个字符可以构成回数(0<=j<=i),那么以j作为分割线得到的最少分割数为cut[j-1]+1。我们只需要找到所有可以构成回数的j并且得出最小值即可。

刚开始,我选择利用hashmap记录所有可能构成回数的情况。即将字符作为key,将字符所在的下标列表作为value。这样每次遍历一个字符时,只需要从hashmap中找到该字符的所有下标并判断该子字符串是否是回数即可。

它有一个很明显的缺陷,就是当出现大量以单一字符构成的回数时,如aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbaaaaaaaaaaaaaaaaaaaaa会超时。很明显还有更好的存储中间结果的方式。

    Map> cache = new HashMap>();
    public int minCut(String s) {
        if(s==null || s.length()==0) return 0;
        int[] minCut = new int[s.length()+1];
        minCut[0] = -1;
        for(int i = 0 ; i list = new ArrayList();
                list.add(i);
                cache.put(cur, list);
            }else{
                int max = minCut[i] + 1;
                List values = cache.get(cur);
                for(int index : values){
                    if(isPalindrome(s.substring(index, i+1))){
                        max = Math.min(max, minCut[index]+1);
                    }
                }
                minCut[i+1] = max;
                values.add(i);
            }
        }
        return minCut[s.length()];
    }
    
    public boolean isPalindrome(String subString){
        if(subString.length()<=1) return true;
        char[] sArray = subString.toCharArray();
        int left = 0,
                right = subString.length()-1;
        while(left
思路二:存储中间的回数结果

这里我们采用二维数组的方式来存储中间的回数结果。我们会新建一个boolean[s.length][s.length] isPalindrome,其中isPalindrome[i][j]代表从i开始j结束的子数组是否是回数。再采用上面所说的方法,利用中间结果得出最小分割次数。

    public int minCut2(String s){
        if(s==null || s.length()<=1) return 0;
        int length = s.length();
        boolean[][] isPalindrome = new boolean[length][length];
        int[] result = new int[length];
        char[] array = s.toCharArray();
        for(int end = 0 ; end=0 ; start--){
                if(array[start] == array[end]){
                    if(end-start<=2){
                        isPalindrome[start][end] = true;
                    }else{
                        isPalindrome[start][end] = isPalindrome[start+1][end-1];
                    }
                }
                
                if(isPalindrome[start][end]){
                    if(start==0){
                        result[end] = 0;
                    }else{
                        result[end] = Math.min(result[start-1]+1, result[end]);
                    }
                }
            }
        }
        return result[length-1];
    }


想要了解更多开发技术,面试教程以及互联网公司内推,欢迎关注我的微信公众号!将会不定期的发放福利哦~

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/68623.html

相关文章

  • [leetcode]132. Palindrome Partitioning II

    摘要:用表示当前位置最少需要切几次使每个部分都是回文。表示到这部分是回文。如果是回文,则不需重复该部分的搜索。使用的好处就是可以的时间,也就是判断头尾就可以确定回文。不需要依次检查中间部分。 Given a string s, partition s such that every substring of the partition is a palindrome. Return the...

    Airy 评论0 收藏0
  • [LintCode] Palindrome Partitioning II

    摘要:假设我们从后向前,分析到第位,开始判断,若为,说明从第位向前到第位的子串是一个回文串,则就等于第位的结果加。然后让继续增大,判断第位到最后一位的范围内,有没有更长的回文串,更长的回文串意味着存在更小的,用新的来替换。 Problem Given a string s, cut s into some substrings such that every substring is a p...

    funnyZhang 评论0 收藏0
  • [Leetcode] Palindrome Partitioning 回文分割

    摘要:深度优先搜素复杂度时间空间思路因为我们要返回所有可能的分割组合,我们必须要检查所有的可能性,一般来说这就要使用,由于要返回路径,仍然是典型的做法递归时加入一个临时列表,先加入元素,搜索完再去掉该元素。 Palindrome Partitioning Given a string s, partition s such that every substring of the parti...

    leanote 评论0 收藏0
  • [LeetCode] 267. Palindrome Permutation II

    Problem Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empty list if no palindromic permutation could be form. Example Given s = aabb, return [abba,baa...

    huashiou 评论0 收藏0
  • LeetCode 攻略 - 2019 年 7 月下半月汇总(100 题攻略)

    摘要:月下半旬攻略道题,目前已攻略题。目前简单难度攻略已经到题,所以后面会调整自己,在刷算法与数据结构的同时,攻略中等难度的题目。 Create by jsliang on 2019-07-30 16:15:37 Recently revised in 2019-07-30 17:04:20 7 月下半旬攻略 45 道题,目前已攻略 100 题。 一 目录 不折腾的前端,和咸鱼有什么区别...

    tain335 评论0 收藏0

发表评论

0条评论

jeyhan

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<