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 words, determine if s can be segmented into a space-separated sequence of one or more dictionary words. You may assume the dictionary does not contain duplicate words.
For example, given
s = "leetcode",
dict = ["leet", "code"].
Return true because "leetcode" can be segmented as "leet code".
思路:
定义状态矩阵dp[i]表示0-i能被切割,需要先找到0-(j -1),然后j -i 这个区间是否是字典里面的,这样找。思路就是序列性动态规划,事件复杂度是n^2.
注意本题的初始化方法,需要从0开始进行查找符合条件的字典字符串,要找到从0位置开始的所有符合条件的字符串,比如go,goal,goals。
class Solution {
public:
bool wordBreak(string s, vector<string>& wordDict) {
if(s.size() == ){
return false;
}
if(wordDict.size() == ){
return ;
}
int n = s.size();
vector<bool> dp(n,false);
//hashset
unordered_set<string> wordSet;
for(int i = ;i < wordDict.size();++i){
wordSet.insert(wordDict[i]);
}
//find first true
int ix = ;
for(ix = ;ix < n;++ix){
if(wordSet.find(s.substr(,ix + )) != wordSet.end()){
dp[ix] = true;
//break;
}
}
// if(ix == n){
// return dp[ix - 1];
// }开始这里没注释,直接每次退出循环ix都等于n,总是出错,因为是原来break掉,才有这句
//funciton
for(int i = ;i < n;++i){
for(int j = ;j <= i;++j){
if((dp[j - ] == true) && (wordSet.find(s.substr(j,i - j + )) != wordSet.end())){
dp[i] = true;
}
}
}
return dp[n - ];
}
};
Word breakII需要找出所有符合条件的分割字符串,并且输出。首先考虑DFS模板,这里的巧妙之处就是start取代了j这个变量,但是复杂度还是平方级别。不熟悉的是string的append,insert,erase(pos,arg),记住是包含pos位置的。参考:水中的鱼
class Solution {
public:
void helper(unordered_set<string> &wordSet,vector<string> &res,string &s,string &tmp,int start){
if(start == s.size()){
res.push_back(tmp.substr(,tmp.size() - ));
}
for(int i = start;i < s.size();++i){
string sub = s.substr(start,i - start + );
if( wordSet.find(sub) == wordSet.end()){
continue;
}
tmp.append(sub).append(" ");//只有每步满足条件之后才开始位置为该步的下一步
helper(wordSet,res,s,tmp,i + );
tmp.erase(tmp.size() - sub.size() - );
}
}
vector<string> wordBreak(string s, vector<string>& wordDict) {
if(s.size() == ){
return {};
}
if(wordDict.size() == ){
return {};
}
vector<string> res;
unordered_set<string> wordSet;
for(string tmp : wordDict){
wordSet.insert(tmp);
}
string tmp;
helper(wordSet,res,s,tmp,);
return res;
}
};
这样有很多重复计算,需要引入一个isOK矩阵,记录之前访问的结果,如果在之前i这个位置已经切割了一次,并且没有找到结果,那么就是false,下次不需要再访问了。这里首先都初始化为true。
int oldSize = res.size();
helper(wordSet,res,s,tmp,i + ,isOk);
if(oldSize == res.size()){
isOk[i] = false;
}
这里理解起来比较困难,记住每次经过helper函数应该是有一个结果压入res中的,但是前后size一样大,所以从i这个元素切割没有的得到结果,那下次切割到这个i的时候,就不需要再计算了,直接跳过。
class Solution {
public:
void helper(unordered_set<string> &wordSet,vector<string> &res,string &s,string &tmp,int start,vector<bool> &isOk){
if(start == s.size()){
res.push_back(tmp.substr(,tmp.size() - ));
}
for(int i = start;i < s.size();++i){
string sub = s.substr(start,i - start + );
if((isOk[i] == false) || wordSet.find(sub) == wordSet.end()){
continue;
}
tmp.append(sub).append(" ");//只有每步满足条件之后才开始位置为该步的下一步
int oldSize = res.size();
helper(wordSet,res,s,tmp,i + ,isOk);
if(oldSize == res.size()){
isOk[i] = false;
}
tmp.erase(tmp.size() - sub.size() - );
}
}
vector<string> wordBreak(string s, vector<string>& wordDict) {
if(s.size() == ){
return {};
}
if(wordDict.size() == ){
return {};
}
vector<string> res;
unordered_set<string> wordSet;
for(string tmp : wordDict){
wordSet.insert(tmp);
}
string tmp;
vector<bool> isOk(s.size(),true);//代表从i位置分割是否能得到一个结果
//isOk[0] = false;
helper(wordSet,res,s,tmp,,isOk);
return res;
}
};
139. Word Break 以及 140.Word Break II的更多相关文章
- 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 单词拆分II
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, add space ...
- LeetCode笔记:140. Word Break II
题目描述 给定一个非空的字符串s,一个非空的字符串list作为字典.通过在s中添加空格可以将s变为由list中的word表示的句子,要求返回所有可能组成的句子.设定list中的word不重复,且每一个 ...
- leetcode 79. Word Search 、212. Word Search II
https://www.cnblogs.com/grandyang/p/4332313.html 在一个矩阵中能不能找到string的一条路径 这个题使用的是dfs.但这个题与number of is ...
- C#操作Word (1)Word对象模型
Word对象模型 (.Net Perspective) 本文主要针对在Visual Studio中使用C# 开发关于Word的应用程序 来源:Understandingthe Word Object ...
- Java实现 LeetCode 140 单词拆分 II(二)
140. 单词拆分 II 给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,在字符串中增加空格来构建一个句子,使得句子中所有的单词都在词典中.返回所有这些可能的句子. 说明: 分 ...
- Java实现 LeetCode 140 单词拆分II
class Solution { public List<String> wordBreak(String s, List<String> wordDict) { List&l ...
- C#中操作Word(1)—— word对象模型介绍
一.开发环境布置 C#中添加对Word的支持,只需添加对Microsoft.Office.Interop.Word的命名空间,如下图所示,右键点击“引用”,在弹出的“添加引用”对话框中选中COM标签页 ...
随机推荐
- 操作系统OS - Zip Bomb
42.zip - A 42 kb zip file that contains 4.5 Petabytes of uncompressed data.
- CentOS修改各大yum源(centos5,centos6,centos7)
备份原配置文件 进入yum源配置目录: cd /etc/yum.repos.d 如果没有先安装wget: sudo yum install wget 修改yum源,就是修改CentOS-Base.re ...
- Navigating to current location ("/") is not allowed
main.js import Router from 'vue-router' // 这个是为了避免一个报错 const originalPush = Router.prototype.push; R ...
- 【原】nginx配置文件
一:下载nginx方式 1.yum install nginx 2.源码安装 二:学习网址 nginx documentation — DevDocs 三:配置文件信息 server { listen ...
- 「luogu4135」作诗
「luogu4135」作诗 传送门 分块好题. 预处理出 \(f[i][j]\) 表示 \(i\) 号块到 \(j\) 号块的答案,\(num[i][k]\) 表示 \(k\) 在前 \(i\) 块的 ...
- 「AMPPZ2014」The Captain
传送门: 这是一道bzoj权限题 Luogu团队题链接 解题思路 直接连边的话边数肯定会爆炸,考虑减少边数. 我们画出坐标系,发现一个东西: 对于两个点 \(A,B\),\(|x_A-y_A|\) 可 ...
- 你知道Verilog HDL程序是如何构成的吗
本节通过硬件描述语言Verilog HDL对二十进制编码器的描述,介绍Verilog HDL程序的基本结构及特点. 二十进制编码器及Verilog HDL描述 二十进制编码器是数字电路中常用的电路单元 ...
- spark aggregateByKey 时 java.lang.OutOfMemoryError: GC overhead limit exceeded
最后发现有一个用户单日访问我们网站次数为 4千万,直接导致 aggregate 时内存不够.过滤掉该用户即可.
- Python学习笔记之基础篇(四)列表与元祖
#### 列表 li = ['alex','wusir','egon','女神','taibai'] ###增加的3种方法 ''' # append li.append('日天') li.append ...
- 如何更改placeholder属性中文字颜色
如何更改placeholder属性中文字颜色 placeholder这个属性是HTML5中新增的属性,该属性的作用是规定可描述输入字段预期值的简短的提示信息,该提示会在用户输入之前显示在输入字段中,会 ...