题目来源:

  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] = Truefor i in range(1,len(s) + 1):for j in range(0,i):if dp[j] and s[j:i] in dict:dp[i] = Truereturn 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

View Code

转载于:https://www.cnblogs.com/chruny/p/5443243.html

[LeetCode]题解(python):140-Word Break II相关推荐

  1. 【DFS + 记忆化递归】LeetCode 140. Word Break II

    LeetCode 140. Word Break II Solution1:我的答案 纯DFS,在第31个case时超时,还是记录一下.. class Solution { // DFS public ...

  2. leetcode 140. Word Break II | 140. 单词拆分 II(动态规划)

    题目 https://leetcode.com/problems/word-break-ii/ 题解 由 leetcode 139. Word Break | 139. 单词拆分(动态规划) 改造而来 ...

  3. 140. Word Break II

    文章目录 1 题目理解 2 回溯+记忆化 1 题目理解 140与130的区别是,当字符串可分的时候,要求返回具体的分割字符串. 2 回溯+记忆化 对于字符串s,如果前面一部分是单词列表中的词,则拆分出 ...

  4. [LeetCode] Word Break II 拆分词句之二

    [LeetCode] Word Break II 拆分词句之二 Given a string s and a dictionary of words dict, add spaces in s to ...

  5. Word Break Word Break II

    leetcode上面的两道动态规划题. 题目1 Word Break Given a string s and a dictionary of words dict, determine if s c ...

  6. LeetCode Word Break II

    Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...

  7. LeetCode: Word Break II [140]

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

  8. 链表基础概念与经典题目(Leetcode题解-Python语言)

    所谓链表,就是由链节点元素组成的表,那什么是链节点呢?直接上定义: class ListNode:def __init__(self, val=0, next=None):self.val = val ...

  9. 哈希表(散列表)基础概念与经典题目(Leetcode题解-Python语言)之中——实际应用

    上一节介绍了哈希表的原理与设计方法,这一节则直接python中现有的哈希表类型:哈希集合 set(集合)和哈希映射 dict(字典)来解决实际应用(刷题). 零.概念 在介绍实际应用之前,有一个概念我 ...

  10. 二叉树层序遍历(广度优先搜索)基础概念与经典题目(Leetcode题解-Python语言)

    二叉树的广度优先搜索即从上到下.从左到右地进行搜索,对于层序遍历(Level Order)问题,即依次遍历第一层节点.第二层节点-等,基本可以秒杀. 广度优先搜索是通过队列来实现的,python中优先 ...

最新文章

  1. 支持Dubbo接口文档生成的工具!
  2. 失眠——耳部按摩(组图)
  3. Lucene 基础理论
  4. matlab 如何hidden,Matlab基本函数-hidden函数
  5. 下个十年的 C 位:物联网趋势大剧透
  6. 机器学习爬大树之决策树(ID3,C4.5)
  7. HDU4907小技巧
  8. 杂:(4)NE555
  9. 高考学文的能报计算机吗,高考志愿填报时,文科生能申报计算机类相关专业吗?...
  10. 35. 内容协商和转码
  11. 五种主流的linux操作系统,五款常用的Linux操作系统
  12. 计算机放样在钢结构应用,计算机放样应用于钢结构工程
  13. 广州地铁集团打造智能财务共享平台加快数字化转型
  14. 高等数学第七版总结(同济大学版)
  15. 如何更改Windows桌面文件夹路径
  16. 常用的绩效管理系统应该具备哪些功能呢?
  17. 18获得触发事件元素节点的方法
  18. MacBook常用快捷键总结
  19. 全能水果柠檬的保健功效和食用方法
  20. 后浪的Java追随者们是什么样的?

热门文章

  1. 在ORACLE中对存储过程加密
  2. 使用Freestreamer 拖动滑动条音频重新播放或者没有声音seekToPosition无效
  3. 动态调用有关的方法?
  4. Xamarin图表开发基础教程(13)OxyPlot框架支持的其它图表
  5. WebDAV服务漏洞利用工具DAVTest
  6. iOS10 UI教程管理层次结构
  7. 在html中不是链接目标属性,在HTML中,()不是链接的目标属性。
  8. delphi中的函数传参如何传枚举参数_shell脚本的函数介绍使用和工作常用案例。建议收藏...
  9. 逆向工程 sql_mybatis逆向生成工具,真的很好用!
  10. 大脑“拖延症”让你直呼“眼瞎”:加工视觉信息有15秒延迟 | Science子刊