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