[leetcode]Word Break II @ Python
原题地址:https://oj.leetcode.com/problems/word-break-ii/
题意:
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word.
Return all such possible sentences.
For example, given
s = "catsanddog"
,
dict = ["cat", "cats", "and", "sand", "dog"]
.
A solution is ["cats and dog", "cat sand dog"]
.
解题思路:这道题不只像word break那样判断是否可以分割,而且要找到所有的分割方式,那么我们就要考虑dfs了。不过直接用dfs解题是不行的,为什么?因为决策树太大,如果全部遍历一遍,时间复杂度太高,无法通过oj。那么我们需要剪枝,如何来剪枝呢?使用word break题中的动态规划的结果,在dfs之前,先判定字符串是否可以被分割,如果不能被分割,直接跳过这一枝。实际上这道题是dp+dfs。
代码:
class Solution:
# @param s, a string
# @param dict, a set of string
# @return a list of strings
def check(self, s, dict):
dp = [False for i in range(len(s)+1)]
dp[0] = True
for i in range(1, len(s)+1):
for k in range(0, i):
if dp[k] and s[k:i] in dict:
dp[i] = True
return dp[len(s)] def dfs(self, s, dict, stringlist):
if self.check(s, dict):
if len(s) == 0: Solution.res.append(stringlist[1:])
for i in range(1, len(s)+1):
if s[:i] in dict:
self.dfs(s[i:], dict, stringlist+' '+s[:i]) def wordBreak(self, s, dict):
Solution.res = []
self.dfs(s, dict, '')
return Solution.res
[leetcode]Word Break II @ Python的更多相关文章
- [leetcode]Word Ladder II @ Python
[leetcode]Word Ladder II @ Python 原题地址:http://oj.leetcode.com/problems/word-ladder-ii/ 参考文献:http://b ...
- LeetCode: 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:Word Break II(DP)
题目地址:请戳我 这一题在leetcode前面一道题word break 的基础上用数组保存前驱路径,然后在前驱路径上用DFS可以构造所有解.但是要注意的是动态规划中要去掉前一道题的一些约束条件(具体 ...
- [LeetCode] Word Break II 拆分词句之二
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...
- LeetCode Word Break II
原题链接在这里:https://leetcode.com/problems/word-break-ii/ 题目: Given a string s and a dictionary of words ...
- [LeetCode] Word Break II 解题思路
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...
- [Leetcode] word break ii拆分词语
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...
- [LeetCode] Word Break II (TLE)
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...
- LeetCode: Word Break II [140]
[题目] Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where ...
随机推荐
- [同步脚本]mysql-elasticsearch同步
公司项目搜索部分用的elasticsearch,那么这两个之间的数据同步就是一个问题. 网上找了几个包,但都有各自的缺点,最后决定还是自己写一个脚本,大致思路如下: 1.在死循环中不断的select指 ...
- Centos部署使用Jexus承载asp.net core2 web应用
一,首先安装本地开发项目用的的 core对应版本运行时: https://www.microsoft.com/net/download/linux-package-manager/centos/run ...
- Codeforces Round #517 (Div. 2, based on Technocup 2019 Elimination Round 2)
Codeforces Round #517 (Div. 2, based on Technocup 2019 Elimination Round 2) #include <bits/stdc++ ...
- Android 9 patch 图片 (.9.png 格式图片) 的特点和制作(转)
本文围绕 .9.png 格式图片讨论以下两个话题: 1. 该格式图片的特点 2. 制作方式 一 .9.png 格式的文件的特点 与传统的png 格式图片相比, 9.png 格式图片在图片四周有一圈一个 ...
- android应用程序签名(转)
概述 Android系统要求,所有的程序经过数字签名后才能安装.Android系统使用这个证书来识别应用程序的作者,并且建立程序间的信任关系.证书不是用于用户控制哪些程序可以安装.证书不需要授权中心来 ...
- Calculate CRC32 as in STM32 hardware (EWARM v.5.50 and later)
http://supp.iar.com/Support/?note=64424&from=note+11927 BackgroundThe STM32 devices from ST Micr ...
- AES CBC/CTR 加解密原理
So, lets look at how CBC works first. The following picture shows the encryption when using CBC (in ...
- Revit Family API 添加对齐
没测试成功,留待以后研究. [TransactionAttribute(Autodesk.Revit.Attributes.TransactionMode.Manual)] ; ; i < nV ...
- 计算机意外地重新启动或遇到错误。windows安装无法继续。若要安装windows 请单击 确定 重新启动计算机
快安装完系统时遇到提示:计算机意外地重新启动或遇到错误.Windows 安装无法继续.若要安装Windows,请单击“确定”重新启动计算机,然后重新启动安装”.如下图所示: 解决办法: 当出现如上提示 ...
- springboot static方法与构造方法加载@VALUE
application.properties文件 mongodb.host=host111 mongodb.port=port222 import org.springframework.beans. ...