[leedcode 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.

public class Solution {
  /*  dp[i][j]保存s(i,j)是否是回文,因为dp[i][j]=dp[i+1][j-1]&&s.charAt(i)==s.charAt(j)(j-i>=2时),即求dp[i][j]需要借助dp[i+1][j-1]求出,所以求dp时,需要从下往上,并且从左向右。
    求出dp之后,需要构造一个res数组,res[i]保存从0到i需要剪切的最少个数。如果dp[0][i]是回文,res[i]=0,如果不是回文,此时已知res[0~i-1]已经求出,因此需要借助res[j](0<=j<i)求res[i]*/
    public int minCut(String s) {
        if(s==null||s.length()<1) return 0;
        int len=s.length();
        int res[]=new int[len];
        boolean dp[][]=new boolean[len][len];
        for(int i=len-1;i>=0;i--){
            for(int j=i;j<len;j++){
                if(s.charAt(i)==s.charAt(j)&&(j-i<2||dp[i+1][j-1])){
                    dp[i][j]=true;
                }
            }
        }
        for(int i=0;i<len;i++){
            int ms=len;
            if(dp[0][i]){
                res[i]=0;
            }else{

                for(int j=0;j<i;j++){
                    if(dp[j+1][i]&&ms>res[j]+1)
                            ms=res[j]+1;
                }
                res[i]=ms;
            }
        }
        return res[len-1];
    }
}
时间: 2024-10-14 00:34:12

[leedcode 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&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

132. Palindrome Partitioning II (String; 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",

Java for 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"

132 Palindrome Partitioning II 分割回文串 II

给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串.返回 s 符合要求的的最少分割次数.例如,给出 s = "aab",返回 1 因为进行一次分割可以将字符串 s 分割成 ["aa","b"] 这样两个回文子串.详见:https://leetcode.com/problems/palindrome-partitioning-ii/description/ class Solution { public: int minCut(str

[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

131. 132. Palindrome Partitioning *HARD* -- 分割回文字符串

131. 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","b"], ["

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&

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