题目描述:

第一次提交:超时 O(N**N)

class Solution:
def wordBreak(self, s: str, wordDict: List[str]) -> List[str]:
if not wordDict:return []
res = []
min_len,max_len = float("inf"),0
for word in wordDict:
min_len = min(len(word),min_len)
max_len = max(len(word),max_len)
res = []
wordDict = set(wordDict)
def helper(s,ans):
if not s:
res.append(ans[:-1])
return
for i in range(min_len,min(len(s)+1,max_len+1)):
if s[:i] in wordDict:
helper(s[i:],ans+s[:i]+" ") helper(s,"")
return res

方法一:记忆化回溯

class Solution:
def wordBreak(self, s: str, wordDict: List[str]) -> List[str]:
import functools
if not wordDict:return []
wordDict = set(wordDict)
max_len = max(map(len, wordDict))
min_len = min(map(len,wordDict))
@functools.lru_cache(None)
def helper(s):
res = []
if not s:
res.append("")
return res
for i in range(min_len-1,len(s)):
if i < max_len and s[:i+1] in wordDict:
for t in helper(s[i+1:]):
if not t:
res.append(s[:i+1])
else:
res.append(s[:i+1] + " " + t)
return res
return helper(s)

另:加个判断,解一也能过

class Solution:
def wordBreak(self, s: str, wordDict: List[str]) -> List[str]:
import functools
if not self.wordBreak2(s,wordDict):return []
if not wordDict:return []
wordDict = set(wordDict)
max_len = max(map(len, wordDict))
min_len = min(map(len,wordDict))
@functools.lru_cache(None)
def helper(s):
res = []
if not s:
res.append("")
return res
for i in range(min_len-1,len(s)):
if i < max_len and s[:i+1] in wordDict:
for t in helper(s[i+1:]):
if not t:
res.append(s[:i+1])
else:
res.append(s[:i+1] + " " + t)
return res
return helper(s) def wordBreak2(self, s: str, wordDict: List[str]) -> bool:
arr,wordSet=[0],set(wordDict)
for i in range(len(s)):
for j in arr[::-1]:
if s[j:i+1] in wordSet:
arr.append(i+1)
break
return arr[-1]==len(s)

leetcode-140-单词拆分②*的更多相关文章

  1. Java实现 LeetCode 140 单词拆分 II(二)

    140. 单词拆分 II 给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,在字符串中增加空格来构建一个句子,使得句子中所有的单词都在词典中.返回所有这些可能的句子. 说明: 分 ...

  2. leetcode 140 单词拆分2 word break II

    单词拆分2,递归+dp, 需要使用递归,同时使用记忆化搜索保存下来结果,c++代码如下 class Solution { public: //定义一个子串和子串拆分(如果有的话)的映射 unorder ...

  3. [LeetCode] 140. 单词拆分 II

    题目链接 : https://leetcode-cn.com/problems/word-break-ii/ 题目描述: 给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,在字符 ...

  4. Java实现 LeetCode 140 单词拆分II

    class Solution { public List<String> wordBreak(String s, List<String> wordDict) { List&l ...

  5. Java实现 LeetCode 139 单词拆分

    139. 单词拆分 给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,判定 s 是否可以被空格拆分为一个或多个在字典中出现的单词. 说明: 拆分时可以重复使用字典中的单词. 你可 ...

  6. Leetcode 139.单词拆分

    单词拆分 给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,判定 s 是否可以被空格拆分为一个或多个在字典中出现的单词. 说明: 拆分时可以重复使用字典中的单词. 你可以假设字典 ...

  7. LeetCode 139. 单词拆分(Word Break)

    139. 单词拆分 139. Word Break

  8. leetcode 139 单词拆分(word break)

    一开始的错误答案与错误思路,幻想直接遍历得出答案: class Solution { public: bool wordBreak(string s, vector<string>& ...

  9. LeetCode——139. 单词拆分

    给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,判定 s 是否可以被空格拆分为一个或多个在字典中出现的单词. 说明: 拆分时可以重复使用字典中的单词. 你可以假设字典中没有重复 ...

  10. [LeetCode] 139. 单词拆分(DP)

    题目 给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,判定 s 是否可以被空格拆分为一个或多个在字典中出现的单词. 说明: 拆分时可以重复使用字典中的单词. 你可以假设字典中没 ...

随机推荐

  1. 游戏game

    1.log4cxx 日志 2.protobuf  数据交互(类似json 3.boost.asio  网络库 4.boost.python 脚本支持 5.语法树 +  c++处理excel资源

  2. 记录我个人对Telegram的了解

    对Telegram(电报) 开始的了解是以为提供了Telegram API,就可以基于它进行开发自己的即时通讯(Instant Messaging)程序. 大概使用过: webogram 和 tele ...

  3. 解决springdatajpa插入大量数据速度慢的问题

    通过看控制台日志可以知道,jpa执行插入的时候每次都会先查询是否存在,然后再一条一条的执行插入,速度相当慢,所以可以jpa和mybatis一起用,用mybatis写原生的sql语句,用过foreach ...

  4. Hadoop(二)HDFS

    海量数据处理 分而治之 核心思想: 把数据分发到多个节点 移动计算到数据附近 计算节点进行本地数据处理 优选顺序,次之随机读 一.HDFS概述 修改,先删除,再重新生成 1.架构 namenode维护 ...

  5. BD贴吧图片爬虫

    #encoding:utf-8 import urllib import urllib.request from lxml import etree class Spider(object): def ...

  6. 【JZOJ6431】【luoguP5658】【CSP-S2019】括号树

    description analysis 用栈维护一下树上路径未匹配的左括号,然后在树上找右括号匹配,设\(f[i]\)为\(i\)节点的贡献,\(g[i]\)是答案 为左括号可以直接继承父节点的信息 ...

  7. demo BaseDao随笔,hibernate框架

    /** * 增加entity * * @param Object对象 * @throws Exception */ public <T> void save(T ob) throws Ex ...

  8. 1、Monkey环境搭建

    步骤: 1.下载adb压缩包: 32位计算机,用这个包:64位计算机,用这个包: 2.把对应的adb压缩包在本地解压,然后把解压后的文件里面的文件夹拷贝到D盘(当然随便你放在哪个目录)根目录,注意路径 ...

  9. This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its de 错误解决

    这是我们开启了bin-log, 我们就必须指定我们的函数是否是1 DETERMINISTIC 不确定的2 NO SQL 没有SQl语句,当然也不会修改数据3 READS SQL DATA 只是读取数据 ...

  10. 分析由Python编写的大型项目(Volatility和Cuckoo)

    之前使用python都是用来做一些简单的脚本,本质上和bat批处理文件没有区别. 但是Python是可以用来编写大型的项目的,比如: Volatility:https://code.google.co ...