题目来源:

  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的更多相关文章

  1. LeetCode笔记:140. Word Break II

    题目描述 给定一个非空的字符串s,一个非空的字符串list作为字典.通过在s中添加空格可以将s变为由list中的word表示的句子,要求返回所有可能组成的句子.设定list中的word不重复,且每一个 ...

  2. leetcode 139. Word Break 、140. Word Break II

    139. Word Break 字符串能否通过划分成词典中的一个或多个单词. 使用动态规划,dp[i]表示当前以第i个位置(在字符串中实际上是i-1)结尾的字符串能否划分成词典中的单词. j表示的是以 ...

  3. 140. Word Break II(hard)

    欢迎fork and star:Nowcoder-Repository-github 140. Word Break II 题目: Given a non-empty string s and a d ...

  4. 【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 ...

  5. 【LeetCode】140. Word Break II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归求解 日期 题目地址:https://leetc ...

  6. [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 ...

  7. 140. Word Break II

    题目: Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where e ...

  8. 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  ...

  9. 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 ...

  10. 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 ...

随机推荐

  1. 得到文件的MD5值

    /// <summary> /// 得到文件的MD5值 /// </summary> /// <param name="Path">文件路径&l ...

  2. asp.net验证控件中常用的正则表达式

    只能输入数字:“^[0-9]*$” 只能输入n位的数字:“^\d{n}$” 只能输入至少n位数字:“^\d{n,}$” 只能输入m-n位的数字:“^\d{m,n}$” 只能输入零和非零开头的数字:“^ ...

  3. C# - 自定义 DataSet 的使用

    -------------------------------------------------  SellProdectManager.cs  -------------------------- ...

  4. Linux学习之echo命令

    语法: # echo [Options] [String] 方括号中的项目是可选的.字符串可以定义为字符的有限序列(如字母,数字,符号,标点符号). 当echo命令不带任何选项或字符串使用时,它会在显 ...

  5. JVM学习之Eclipse输出GC日志

    Java应用启动时,可以通过设置verbose参数来输出JVM的gc情况,命令如下:-verbose:gc或者-XX:+PrintGC在Eclipse中可以通过Run As|Run Configura ...

  6. 关于Centos Linux系统安装Python的问题

    由于最近在研究Python框架Django的使用,安装django扩展没有问题 新建项目  django-admin startproject projectName  如果什么都不修改或者直接创建一 ...

  7. mysql 存储过程中的declare 和 set @的两种变量的区别

    两者在手册中的说明: DECLARE var_name[,...] type [DEFAULT value]这个语句被用来声明局部变量.要给变量提供一个默认值,请包含一个DEFAULT子句.值可以被指 ...

  8. Android 利用剪切板(clipboardManager )实现数据传递

    首先是系统剪切板的调用服务: ClipboardManager ClipboardManager=getSystemService(Context.CLIPBOARD_SERVICE); 然后是写入, ...

  9. SQL复杂查询(子查询)

    USE STUDY SELECT * from EMP SELECT * FROM SALGRADE --1.查询雇员姓名,所在部门编号和名称 SELECT ename,EMP.deptno,DEPT ...

  10. Android消息机制不完全解析(下)

    接着上一篇文章Android消息机制不完全解析(上),接着看C++部分的实现. 首先,看看在/frameworks/base/core/jni/android_os_MessageQueue.cpp文 ...