LeetCode 题解(90): Palindrome Partitioning II

来源:互联网 发布:出租屋网络不稳定 编辑:程序博客网 时间:2024/05/28 11:30

题目:

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