1. 程式人生 > >LeetCode 132. Palindrome Partitioning II

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”,”b”] could be produced using 1 cut.
題目分析:給出一個字串,將該字串分割,使得每部分都是迴文串,求最小的額切割次數。

假設dp[i][j]表示s[i…j]的最小切割次數,則有

dp[i][j]=0s[i]!=s[j]min{dp[i][j],dp[i][k]+dp[k+1][j]+1}i=ji+1=jik<j

程式碼如下:

class Solution {
public:
     int minCut(string s) {
        int n = s.length();
        int dp[n][n];
        memset(dp,0,sizeof(dp));

        for(int i = 0; i < n - 1; i++)
        {
            if
(s[i] == s[i+1]) dp[i][i+1] = 0; else dp[i][i+1] = 1; } for(int len = 2; len <= n; len++) { for(int i = 0; i + len < n; i++) { int j = i + len; dp[i][j] = n; if(s[i] == s[j] && dp[i+1
][j-1] == 0) dp[i][j] = 0; for(int k = i; k < j; k++) { dp[i][j] = min(dp[i][j], dp[i][k] + dp[k+1][j] + 1); } } } return dp[0][n-1]; } };

結果,超時了。後來看了大神的程式碼,增加了一個數組,只用了O(n^2)的複雜度。
程式碼如下:

class Solution {
public:
    int minCut(string s) {
        int n = s.length();
        bool isPal[n][n];
        memset(isPal,0,sizeof(isPal));
        int cut[n];


        for(int j = 0; j < n; j++) {
            cut[j] = j;
            for(int i = 0; i <= j; i++) {
                if(s[i] == s[j] && (j - i <= 1 || isPal[i+1][j-1] == true) ) {
                    isPal[i][j] = true;

                    if(i > 0) {
                        cut[j] = min(cut[j],cut[i-1] + 1);
                    }
                    else {
                        cut[j] = 0;
                    }
                }
            }

        }

        return cut[n-1];
    }
};

對於每個cut[j],同樣是將[0…j]分成兩部分,只不過,其中一部分是迴文子串。
這道題和Leetcode 279. Perfect Squares的技巧很相似。

相關推薦

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 p

python leetcode 132. Palindrome Partitioning II

思路:如果s[left:right]是迴文並且s[:left]也是迴文,那麼s[:right]即是一個分割。同理如果dp[left:right]是最小回文分割並且dp[:left]也是最小回文分割,那麼dp[:right]=dp[left:right]+dp[:left]。為了方便遍歷,這裡

演算法作業第十週(leetcode)——132.palindrome-partitioning-ii

其實已經不是特別想做動態規劃的題了,但是還是抽到了一道動態規劃的題。下面給出題目描述: 這題的大意是給一個字串,要經過最少多少次分割才能把字串分成迴文串組成的字串。 其實這題動態規劃的部分並不是特別難的部分。很容易就可以想到用cut[i]來表示前i個字元所能分成的迴文

DP動態規劃專題六 :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 partit

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 pal

LeetCode 132.Palindrome Partitioning II (分割回文串 II)

題目描述: 給定一個字串 s,將 s 分割成一些子串,使每個子串都是迴文串。 返回符合要求的最少分割次數。 示例: 輸入: "aab" 輸出: 1 解釋: 進行一次分割就可將 s 分割成 ["aa","b"] 這樣兩個迴文子串。 AC C++ Solution:

LeetCode132. Palindrome Partitioning II

題目是這樣: Given a string s, partition s such that every substring of the partition is a palindrome. Return the minimum cuts needed fo

132. Palindrome Partitioning II

rom urn [] for public char min () int class Solution { public int minCut(String s) { int[] dp=new int[s.length()+1];

132 Palindrome Partitioning II 分割回文串 II

回文 回文串 tco ++ ali 例如 post 字符 code 給定一個字符串 s,將 s 分割成一些子串,使每個子串都是回文串。返回 s 符合要求的的最少分割次數。例如,給出 s = "aab",返回 1 因為進行一次分割可以將字符串 s 分割成 ["aa","b"]

DP動態規劃專題四 :LeetCode 132. Palindrome Partitioning

LeetCode 132. Palindrome Partitioning Given a string s, partition s such that every substring of the partition is a palindrome. Return all pos

LeetCode 132. 分割回文串 IIPalindrome Partitioning II

題目描述   給定一個字串 s,將 s 分割成一些子串,使每個子串都是迴文串。 返回符合要求的最少分割次數。 示例: 輸入: "aab" 輸出: 1 解釋: 進行一次分割就可將 s 分割成 ["aa","b"] 這樣兩個迴文子串。 &nb

LeetCode】#132分割回文串II(Palindrome Partitioning II)

【LeetCode】#132分割回文串II(Palindrome Partitioning II) 題目描述 給定一個字串 s,將 s 分割成一些子串,使每個子串都是迴文串。 返回符合要求的最少分割次數。 示例 輸入: “aab” 輸出: 1 解釋: 進行一次分割就可將 s

LeetCode】121.Palindrome Partitioning II

題目描述(Hard) Given a string s, partition s such that every substring of the partition is a palindrome. Return the minimum cuts needed

[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 o

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 palindr

LeetCode Palindrome Partitioning II

這道題與相似。用DP來做,需要保留的歷史資訊就是到當前點能分成幾塊Palindrome, 用一個int陣列res保留。每次更新res[i+1], 比較i+1和res[j]+1大小,取小的。因為單個letter肯定是palindrome, 所以肯定會被字典拆開,但[j.

leetcode: palindrome-partitioning-ii

題目描述: Given a string s, partition s such that every substring of

leetcode dfs Palindrome Partitioning

oid -- ati ++ eve -a cto size oss Palindrome Partitioning Total Accepted: 21056 Total Submissions: 81036My Submissions Given a

[LintCode] Palindrome Partitioning II

com set list could private produced sin ray star Given a string s, cut s into some substrings such that every substring is a palindrome

分割回文串 II · Palindrome Partitioning II

class nbsp 英文 長度 總結 正常 bsp 風格 思路 [抄題]: 給定一個字符串s,將s分割成一些子串,使每個子串都是回文。 返回s符合要求的的最少分割次數。 [思維問題]: [一句話思路]: [輸入量]:空: 正常情況:特大:特小:程序裏處理到的特殊情況:異常