码迷,mamicode.com
首页 > 其他好文 > 详细

132 Palindrome Partitioning II

时间:2015-08-05 06:26:36      阅读:110      评论:0      收藏:0      [点我收藏+]

标签:

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]

 

132 Palindrome Partitioning II

标签:

原文地址:http://www.cnblogs.com/dapanshe/p/4703578.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!