LeetCode: Palindrome Partitioning II [132]

【题目】

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, 返回最小切割次数,是的所有的切割子串都为回文。

【思路】

DP,依次确定s[1...n]的最小分割次数

假设用minCuts[i]表示,以第i结尾的字符串的最小切分次数。

那么怎么确定minCuts[i]. 我们已知了minCuts[j] j=0,1,...,i-1

对于i之前的每个位置j, 如果s[j+1,..,i]为回文串,则j为切割点的最小切割次数就是minCuts[j]+1;

所以我们要从所有的j中取一个位置使得i位置的切割次数最小的位置作为切割点,即minCuts[i]=min{minCuts[j]+1} s[j+1,..,i]是回文,j=0,1...i-1

【代码】

class Solution {
public:
    int minCut(string s) {
        int len=s.length();
        if(len==0||len==1)return 0;
        //构造回文判别矩阵
        vector<vector<bool> > isPal(len, vector<bool>(len, false));
        //初始化isPal[i][i]
        for(int i=0; i<len; i++)
            isPal[i][i]=true;
        //初始化isPal[i][i+1]
        for(int i=0; i<len-1; i++)
            if(s[i]==s[i+1])isPal[i][i+1]=true;
        //初始化其他i<j的子串
        for(int i=len-3; i>=0; i--)
            for(int j=i+2; j<len; j++)
                isPal[i][j]=(s[i]==s[j])&&isPal[i+1][j-1];

        //求最小切割
        vector<int> minCuts(len, 0);  //记录以i结尾的子串的最小切分数
        for(int i=0; i<len; i++){
            int mincut=INT_MAX;
            for(int j=-1; j<i; j++){
                if(isPal[j+1][i]){
                    if(j==-1)mincut=0;
                    else if(minCuts[j]+1<mincut)mincut=minCuts[j]+1;
                }
            }
            if(mincut!=INT_MAX)minCuts[i]=mincut;
        }
        return minCuts[len-1];
    }
};

LeetCode: Palindrome Partitioning II [132]

时间: 2024-08-24 13:25:43

LeetCode: Palindrome Partitioning II [132]的相关文章

[LeetCode] Palindrome Partitioning II [12]

题目 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&qu

[LeetCode] Palindrome Partitioning II (DP)

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"

LeetCode: 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. For example, given s = "aab",Return 1 since the palindrome pa

[LeetCode] 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",

leetcode -- 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

[LeetCode] 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"

[LeetCode]132.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&qu

[email&#160;protected] [131/132] Palindrome Partitioning &amp; Palindrome Partitioning II

https://leetcode.com/problems/palindrome-partitioning/ Given a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s. For example, given s = "aab",Return [ ["aa&qu

132 Palindrome Partitioning II

132 Palindrome Partitioning II 这道题就是标识出s[i:j+1]是否为palindrome, 然后dp找出最小分割 class Solution: # @param {string} s # @return {integer} def minCut(self, s): LS = len(s) dp = [[False] * LS for i in range(0, LS)] ans = [1<<31 for i in range(0 ,LS)] for ls in