LeetCode题解(0139):单词拆分(Python)
发布日期:2021-06-29 20:16:00 浏览次数:3 分类:技术文章

本文共 742 字,大约阅读时间需要 2 分钟。

题目:(中等)

标签:动态规划、滑动窗口、字符串

解法 时间复杂度 空间复杂度 执行用时
Ans 1 (Python) O ( N 3 + D ) O(N^3+D) O(N3+D) O ( D + N ) O(D+N) O(D+N) 36ms (97.32%)
Ans 2 (Python)
Ans 3 (Python)

解法一:

class Solution:    def wordBreak(self, s: str, wordDict: List[str]) -> bool:        if not wordDict:            return False        words = set(wordDict)        max_length = max(len(word) for word in words)        window = collections.deque([0])        for i2 in range(1, len(s) + 1):            if i2 - window[0] > max_length:                window.popleft()            if not window:                return False            for i1 in window:                if s[i1:i2] in words:                    window.append(i2)                    break        return window[-1] == len(s)

转载地址:https://dataartist.blog.csdn.net/article/details/112604169 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:LeetCode题解(0174):地下城游戏(Python)
下一篇:LeetCode题解(0137):只出现一次的数字II(Python)

发表评论

最新留言

表示我来过!
[***.240.166.169]2024年05月01日 18时31分58秒