• 边工作边刷题:70天一遍leetcode: day 11-4


    Word Break I/II

    现在看都是小case题了,一遍过了。注意这题不是np complete,dp解的time complexity可以是O(n^2) or O(nm)。所有解的个数是exponential的 (比如”aaaa....”, dict="a, aa”)。以前在amazon onsite遇到过,不过既不是返回True/False,也不是所有解,而是一个解。其实一个解和True/False是一个复杂度,因为单一解是可以从dp反向重构的。

    class Solution(object):
        def wordBreak(self, s, wordDict):
            """
            :type s: str
            :type wordDict: Set[str]
            :rtype: bool
            """
            n = len(s)
            dp = [False]*(n+1)
            dp[0]=True
            for i in range(1, n+1):
                for j in range(i):
                    if s[j:i] in wordDict and dp[j]:
                        dp[i]=True
                        
            return dp[n]
    
  • 相关阅读:
    jQuery-1.样式篇---属性与样式
    jQuery-1.样式篇---选择器
    jQuery-1.样式篇
    随机数
    UIButton
    UILabel
    webView
    气泡聊天
    下拉和上拉刷新
    LimitDemo
  • 原文地址:https://www.cnblogs.com/absolute/p/5675816.html
Copyright © 2020-2023  润新知