[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 ...
随机推荐
- C#:占位符的例子
在c#中有两种方式可以输出多个字符. static void Main() { string c=Console.ReadLine(); string d=Console.ReadLine(); Co ...
- Unity截图
什么都不说了,直接上代码. using UnityEngine; using System.Collections; using System.IO; public class CutImage : ...
- js 控制台的错误提示
错误:程序运行过程中发生的异常状态 导致程序停止运行——异常 错误处理:当程序发生错误时,保证程序不退出的机制 发生错误时,程序会自动创建一个Error对象: Error对象中仅封装了错误的信息 js ...
- 剑指offer第10题
import java.util.Scanner; /* 前两种方法是看最低为是不是为1,不为1则向右移动. 第一种只能对正整数有效,对负数不行,因为负数用的是补码,最高外符号位为1,最后右移动,肯定 ...
- (跨平台)cocos2d-x C++ or Object-C(前端)调用C# webservices(后台),实现交叉编译到Android/IOS/WinPhone等移动终端设备
1.2014年4月2号算是正式找到自己的实习工作-杭州美迪软件有限公司(移动物联事业部)合作于:四川管家婆总部移动终端代理,由于在校选编程专业语言C#和在浙大网新培训课程(C#.Asp.net开发)缘 ...
- Nginx与Tomcat安装、配置与优化
Nginx与Tomcat安装.配置与优化 Nginx与Tomcat安装.配置与优化 Nginx的安装与使用 Nginx是一款优秀的反向代理服务器 安装: rpm(或者是pkg安装),是预编译好的程序包 ...
- raphael 支持group(简)
raphael 不支持group,里面有的set方法,只是把对象数组存起来,方法调用的时候,遍历都调用下,但是在实际需求上面感觉group还是瞒有用处的,可以控制group下面的节点的交互 比如地图区 ...
- Scala类型声明与定义、函数定义、流程控制、异常处理
Scala源代码被编译成Java字节码,所以它可以运行于JVM之上,并可以调用现有的Java类库. Scala的基础类型基本与javascript一致. Scala的数据类型全部相同于Java中,具有 ...
- 自绘Tab控件
自绘tab按钮效果图如下: 使用例子: MyTabControl *tabControl = NULL; tabControl = new MyTabControl();tabControl-> ...
- jQuery.fn和jQuery.prototype jquery.extend() jquery.fn.extend()区别介绍
这里的 jQuery , jQuery.fn , jQuery,fn,init ,jQuery,prototype 都代表什么. 来看下jQuery的源码是怎么样定义的: (function( win ...