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 range(0, LS):
            for i in range(0, LS-ls):
                if s[i] == s[i+ls] and (ls <= 2 or dp[i+1][i+ls-1]):
                    dp[i][i+ls] = True
        ans[0] = 0
        for i in range(1, LS):
            for j in range(0,i+1):
                if dp[j][i]:
                    ans[i] = 0 if j == 0 else (min(ans[i], 1+ans[j-1]))
        return ans[-1]
时间: 2024-10-13 22:27:06

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 (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"

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

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