[LeetCode]题解(python):140-Word Break II
题目来源:
https://leetcode.com/problems/word-break-ii/
题意分析:
给定一个字符串s和一个字典dict(set),将所有将s有字典dict组成的结果输出。比如s = "catsanddog",
dict = ["cat", "cats", "and", "sand", "dog"].那么结果是["cats and dog", "cat sand dog"]。
题目思路:
我们将问题细化,如果s[:i]在字典dict里面,那么结果就是s[:i]和 solve(s[i + 1:],dict)的笛卡尔乘积。如果直接实现,那么时间复杂度较高,所以这里用动态规划的思想,判断一个字符串是否可以有字典组成。
代码(python):
class Solution(object):
def isp(self,s,dict):
dp = [False for i in range(len(s) + 1)]
dp[0] = True
for i in range(1,len(s) + 1):
for j in range(0,i):
if dp[j] and s[j:i] in dict:
dp[i] = True
return dp[len(s)]
def wordBreak(self, s, wordDict):
"""
:type s: str
:type wordDict: Set[str]
:rtype: List[str]
"""
ans,tmp = [],""
if s in wordDict:
ans.append(s)
for i in range(len(s)):
tmp += s[i]
if tmp in wordDict:
if self.isp(s[i + 1:],wordDict):
t = self.wordBreak(s[i+1:],wordDict)
for j in t:
ans.append(tmp + " " + j)
return ans
[LeetCode]题解(python):140-Word Break II的更多相关文章
- LeetCode笔记:140. Word Break II
题目描述 给定一个非空的字符串s,一个非空的字符串list作为字典.通过在s中添加空格可以将s变为由list中的word表示的句子,要求返回所有可能组成的句子.设定list中的word不重复,且每一个 ...
- leetcode 139. Word Break 、140. Word Break II
139. Word Break 字符串能否通过划分成词典中的一个或多个单词. 使用动态规划,dp[i]表示当前以第i个位置(在字符串中实际上是i-1)结尾的字符串能否划分成词典中的单词. j表示的是以 ...
- 140. Word Break II(hard)
欢迎fork and star:Nowcoder-Repository-github 140. Word Break II 题目: Given a non-empty string s and a d ...
- 【LeetCode】140. Word Break II
Word Break II Given a string s and a dictionary of words dict, add spaces in s to construct a senten ...
- 【LeetCode】140. Word Break II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归求解 日期 题目地址:https://leetc ...
- [LeetCode] 140. Word Break II 单词拆分II
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, add space ...
- 140. Word Break II
题目: Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where e ...
- 139. Word Break 以及 140.Word Break II
139. Word Break Given a non-empty string s and a dictionary wordDict containing a list of non-empty ...
- leetcode 140. Word Break II ----- java
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...
- Java for LeetCode 140 Word Break II
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...
随机推荐
- CentOS7与Win7双系统引导问题
先安装的Win7,后安装的CentOS7,结果系统引导就只有CentOS7了.记得以前CentOS6.x系列没这个问题,主要是由于CentOS7.x使用grub2的原因吧. 方案一:使用Win PE. ...
- Apache Tomcat Not Showing in Eclipse Server Runtime Environments
In my case I needed to install "JST Server Adapters". I am running Eclipse 3.6 Helios RCP ...
- arcgis切图问题
头一次用ArcGIS Server切图,所以遇到问题总是摸不着头脑,网上一个劲的搜罗,可惜ArcGIS Server使用的资料实在太少,所以只好自己憋,或者问客服了.切图今天积累了不少了经验,记录下, ...
- win32 控件的创建和消息响应
1. 控件的创建 控件的创建和窗口创建是一样的,例如: ,,,, hWnd,(HMENU)IDB_BUTTON01,hInst,NULL); 是一个按钮的创建,其中hWnd是窗口句柄,hInst是应用 ...
- BZOJ 4016: [FJOI2014]最短路径树问题( 最短路 + 点分治 )
先跑出最短路的图, 然后对于每个点按照序号从小到大访问孩子, 就可以搞出符合题目的树了. 然后就是经典的点分治做法了. 时间复杂度O(M log N + N log N) -------------- ...
- QF——iOS代理模式
iOS的代理模式: A要完成某个功能,它可以自己完成,但有时出于一些原因,不方便自己完成.这时A可以委托B来帮其完成此功能,即由B代理完成.但是这个功能不是让B随随便便任其完成.此时,会有一个协议文件 ...
- 如何将js与HTML完全脱离
先举出一个例子: var sound='Roar!'; function myOrneryBeast(){ alert(this); this.style.color='green';//this指代 ...
- Blast使用详解
Blast,全称Basic Local Alignment Search Tool,即"基于局部比对算法的搜索工具",由Altschul等人于1990年发布.Blast能够实现比较 ...
- linux使用工具记录
linux工具查询手册: http://linuxtools-rst.readthedocs.io/zh_CN/latest/index.html
- sar
雷达卫星数据产品介绍(一) — ERS 卫星 ERS-1 ERS-2 欧空局分别于 1991 年和 1995 年发射. 携带有多种有效载荷, 包括 侧视合成孔径雷达(SAR)和风向散射计等装置),由于 ...