140. 单词拆分 II
Q:
给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,在字符串中增加空格来构建一个句子,使得句子中所有的单词都在词典中。返回所有这些可能的句子。
说明:
分隔时可以重复使用字典中的单词。
你可以假设字典中没有重复的单词。
示例 1:
输入:
s = “catsanddog”
wordDict = [“cat”, “cats”, “and”, “sand”, “dog”]
输出:
[
“cats and dog”,
“cat sand dog”
]
示例 2:
输入:
s = “pineapplepenapple”
wordDict = [“apple”, “pen”, “applepen”, “pine”, “pineapple”]
输出:
[
“pine apple pen apple”,
“pineapple pen apple”,
“pine applepen apple”
]
解释: 注意你可以重复使用字典中的单词。
示例 3:
输入:
s = “catsandog”
wordDict = [“cats”, “dog”, “sand”, “and”, “cat”]
输出:
[]
A:
关键是要先用139的代码算一下给的字符串能不能被拆分为字典里的单词,不能直接返回。因为算是否能被拆分是O(N ^ 2),而本题要求的所有序列是O(N ^ 3),不先判断一下有一个全是’a’的用例是过不去的。。
C++:
class Solution {
public:
vector<string> wordBreak(string s, vector<string>& wordDict) {
vector<vector<string>> dp(s.size(),vector<string>());
unordered_set<string> dic;
for(auto& str:wordDict){
dic.insert(str);
}
vector<bool> judge(s.size(),false);
for(int i=0;i<s.size();++i){
if(dic.count(s.substr(0,i+1))){judge[i]=true;}
else{
for(int j=0;j<=i;++j){
if(judge[j] and dic.count(s.substr(j+1,i-j))){
judge[i]=true;
break;
}
}
}
}
if(not judge.back()){return {};}
//dp[i]表示s截止到i的可能分割出的句子
for(int i=0;i<s.size();++i){
for(int j=0;j<=i;++j){
if(dic.count(s.substr(j,i-j+1))){
string str2=s.substr(j,i-j+1);
if(j==0){
dp[i].push_back(str2);
continue;
}
if(not judge[j-1]){continue;}
for(auto& str1:dp[j-1]){
dp[i].push_back(str1+" "+str2);
}
}
}
}
return dp[s.size()-1];
}
};
python:
class Solution:
def wordBreak(self, s: str, WordDict):
l=len(s)
if not l:
return 0
dic={ x:1 for x in WordDict}
#上一题代码拿来用
judge=[0 for i in range(l)]
judge[0]=int(s[0] in dic) #i==0递推基础
for i in range(1,l): #i==1开始遍历
if s[:i+1] in dic:
judge[i]=1
else:
for j in range(i):
if judge[j] and s[j+1:i+1] in dic:
judge[i]=1
break
print(judge)
if not judge[-1]:
return []
#开始算本题所求的序列
dp=[[]for i in range(l)] #dp[i]是一个列表,存储不同的s[1,i]闭区间的有效拆分序列
if s[0] in dic:
dp[0].append(s[0]) #递推起始状态
for i in range(1,l): #从i==1开始自底向上递推
if s[:i+1] in dic: #s[0:i+1]本身就在字典里则加入解集
dp[i].append(s[:i+1])
for cut in range(i): #进行切分,符合条件的加入解集
if s[cut+1:i+1] in dic and judge[cut]:
#cut前面的子问题有解且cut之后到i的字符串在字典中
for x in dp[cut]: #x为字符串
dp[i].append(x+' '+s[cut+1:i+1])
#把dp[cut]中的每个解加上cut后的字符串,插入dp[i]的解集
# print(dp)
return dp[-1]
140. 单词拆分 II的更多相关文章
- Java实现 LeetCode 140 单词拆分 II(二)
140. 单词拆分 II 给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,在字符串中增加空格来构建一个句子,使得句子中所有的单词都在词典中.返回所有这些可能的句子. 说明: 分 ...
- [LeetCode] 140. 单词拆分 II
题目链接 : https://leetcode-cn.com/problems/word-break-ii/ 题目描述: 给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,在字符 ...
- Java实现 LeetCode 140 单词拆分II
class Solution { public List<String> wordBreak(String s, List<String> wordDict) { List&l ...
- [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 ...
- leetcode 140 单词拆分2 word break II
单词拆分2,递归+dp, 需要使用递归,同时使用记忆化搜索保存下来结果,c++代码如下 class Solution { public: //定义一个子串和子串拆分(如果有的话)的映射 unorder ...
- 算法——单词拆分 II
给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,在字符串中增加空格来构建一个句子,使得句子中所有的单词都在词典中.返回所有这些可能的句子. 链接: leetcode. 解题思路 ...
- [Swift]LeetCode140. 单词拆分 II | Word Break II
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, add space ...
- [LeetCode] 139. Word Break 单词拆分
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...
- leetcode 139. Word Break 、140. Word Break II
139. Word Break 字符串能否通过划分成词典中的一个或多个单词. 使用动态规划,dp[i]表示当前以第i个位置(在字符串中实际上是i-1)结尾的字符串能否划分成词典中的单词. j表示的是以 ...
随机推荐
- Qt实践-基于C++的UI设计基础
学习网址:http://c.biancheng.net/view/1824.html 我的代码: 头文件qwdlgmanual.h: #ifndef QWDLGMANUAL_H #define QWD ...
- 给你的HTTPS添加Let's Encrypt证书
Let’s Encrypt setup for Apache, NGINX & Node.js https://gist.github.com/davestevens/c9e437afbb41 ...
- vue项目中使用element ui上传图片到七牛
1.获取token值 后台有接口调用直接返回token值 //请求后台拿七牛云token async getQiniuToken() { //token let uploadtoken = await ...
- 《NVM-Express-1_4-2019.06.10-Ratified》学习笔记(6.5-加-8.2-加-8.3)End-to-end_Data_Protection
6.5 End-to-end Protection Information 端到端保护信息 包含数据转存的命令可以使用端到端数据保护.在这些命令内部[位于Command Dword 12 的 [29: ...
- C语言实现链式二叉树静态创建,(先序遍历),(中序遍历),(后续遍历)
#include <stdio.h>#include <stdlib.h> struct BTNode{ char data ; struct BTNode * pLchild ...
- 南昌邀请赛B题(拉格朗日插值)
题目链接:https://nanti.jisuanke.com/t/40254 #include<iostream> #include<cstdio> #include< ...
- 4 Values whose Sum is 0 UVA 1152
题目链接:https://vjudge.net/problem/UVA-1152 这题题意就是在四个集合内.每个集合分别里挑一个数a,b,c,d,求a+b+c+d=0有多少种选法. 暴力的话就是四重循 ...
- 链表问题----删除链表的中间节点和a/b处的节点
删除链表的中间节点和a/b处的节点 对于给定一个链表的头节点head,实现删除链表的中间节点的函数. 例如 不删除任何节点: 1->2,删除节点1 1->2->3,删除节点2 1-& ...
- Java8之Stream详解
Java8中提供了Stream对集合操作作出了极大的简化,学习了Stream之后,我们以后不用使用for循环就能对集合作出很好的操作. 一.流的初始化与转换 Java中的Stream的所有操作 ...
- Laradock + tp5 + nginx 配置虚拟机域名始终跳转首页/502报错
laradock默认配置文件如下: 配置运用于本地windows+phpstudy 部署的laravel项目未出现问题,如下: server { listen ; listen [::]:; serv ...