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

139 Word Break

时间:2015-07-22 06:58:25      阅读:111      评论:0      收藏:0      [点我收藏+]

标签:

139 Word Break

纯dp

class Solution:
    # @param s, a string
    # @param dict, a set of string
    # @return a boolean
    def wordBreak(self, s, dict):
        length = len(s)
        dp = [False]*(length + 1)
        dp[0] = True
        i = 1
        while i < length+1:
            j = i-1
            while j >= 0:
                cur = s[j:i]
                if dp[j] and cur in dict:
                    dp[i] = True
                    break
                j -= 1
            i += 1
        return dp[length]

 

139 Word Break

标签:

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

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