LeetCode 题解(90): 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.

For example, given s = "aab",
Return 1 since the palindrome partitioning ["aa","b"] could be produced using 1 cut.

题解:

动态规划是一定的。先记录最坏的cut情况,即到第i个字母为止需要切 i 刀。比如s = "abcdefg",则i = 0时需要切0刀,i=1时需要切1刀,以此类推。存在mincut[]中,待以后更新。

然后通过判断是否为Palindrome来更新mincut。首先初始化一个二维矩阵palindrome[][],其中每个元素均为false。此矩阵作为判断palindrome的动态规划查表。

递推公式为:for i = [1,  s.length()-1], j = [0, i]

if j - 1 >= 0:

mincut[i] = min(mincut[i], mincut[j-1] + 1) 

else:

mincut[i] = 0

其中并不是每个j都需要更新mincut[],而只有当s[j:i]为Palindrome的时候才需计算,由此省去大量计算时间。判断s[j:i]为Palindrome的条件为:

if s[j] == s[i] and (i - j < 2 or palindrome[j+1][i-1] == true) (此处使用了动态规划查表palindrome,又省去了大量时间)。


C++版:

class Solution {
public:
    int minCut(string s) {
        if(s.length() <= 1)
            return 0;
        vector<int> mincut(s.length());
        for(int i = 0; i < s.length(); i++) {
            mincut[i] = i;
        }
        vector<vector<bool>> palindrome;
        for(int i = 0; i < s.length(); i++) {
            vector<bool> row(s.length());
            palindrome.push_back(row);
        }
        
        for(int i = 1; i < s.length(); i++) {
            for(int j = i; j >= 0; j--) {
                if(s[j] == s[i] && (i - j < 2 || palindrome[j+1][i-1])) {
                    palindrome[j][i] = true;
                    if(j - 1 >= 0)
                        mincut[i] = min(mincut[i], mincut[j-1]+1);
                    else
                        mincut[i] = 0;
                }
            }
        }
        return mincut[s.length()-1];
    }
};

Java版:

public class Solution {
    public int minCut(String s) {
        if(s.length() == 0)
            return 0;
            
        int[] mincut = new int[s.length()];
        for(int i = 0; i < s.length(); i++)
            mincut[i] = i;
            
        boolean[][] palindrome = new boolean[s.length()][s.length()];
        
        for(int i = 1; i < s.length(); i++) {
            for(int j = i; j >= 0; j--) {
                if(s.charAt(i) == s.charAt(j) && (i - j < 2 || palindrome[j+1][i-1])) {
                    palindrome[j][i] = true;
                    if(j - 1 >= 0)
                        mincut[i] = Math.min(mincut[i], mincut[j-1] + 1);
                    else
                        mincut[i] = 0;
                }
            }
        }
        
        return mincut[s.length()-1];
    }
}

Python版:

class Solution:
    # @param {string} s
    # @return {integer}
    def minCut(self, s):
        if len(s) == 0:
            return 0
            
        mincut = [i for i in range(len(s))]
        palindrome = []
        for i in range(len(s)):
            palindrome.append([False] * len(s))
            
        for i in range(1, len(s)):
            for j in range(i,-1,-1):
                if s[i] == s[j] and (i - j < 2 or palindrome[j+1][i-1]):
                    palindrome[j][i] = True
                    if j - 1 >= 0:
                        mincut[i] = min(mincut[i], mincut[j-1] + 1)
                    else:
                        mincut[i] = 0
                        
        return mincut[len(s)-1]
                    


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值