[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 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"].
这题没有AC,但是做出了递归回朔的版本,最后一个一串a后面跟个b的case超时了没法通过。
sand -- [dog] ->"cat sand dog"
/
cat --[sanddog]
/
[catsanddog]
\
cats --[anddog]
\
and -- [dog] -> "cats and dog"
从这个结构可以看出,这个问题可以被分解为当前的问题+更小规模的同类子问题,用递归求解。
我用了一个vector<stirng>来记录每个阶段(路径)上的结果,当搜索完字符串的时候(left >= right)意味着数组里的就是一个结果。
void wb(string s, int left, int right, unordered_set<string> &dict, vector<string> t, vector<string> &re) {
if (s.length() <= )
return;
if (left >= right) {
string k;
for (int i=; i<t.size(); i++) {
if (i != t.size()-) {
k = k + t[i] + " ";
}
else
k+=t[i];
}
re.push_back(k);
return;
}
//find matches
for (const auto& elem: dict) {
int len = (int)((string)elem).size();
if (left+len > s.length()) continue;
string sub = s.substr(left, len);
if (elem.compare(sub) == ) {
t.push_back(sub);
wb(s, left+len, right, dict, t, re);
if (t.size())
t.pop_back();
}
}
}
vector<string> wordBreak(string s, unordered_set<string> &dict) {
vector<string> ret;
vector<string> t;
wb(s, , s.size()-, dict,t, ret);
return ret;
}
在编写过程中遇到的两个bug都是因为没有很好理解程序造成的,第一个是t数组,一开始我传起引用,结果发现最后结果集翻倍。。。 第二个是调用wb后 t 没有pop,这样就把前一个分支的内容带到了下一分支造成错乱。
求大神指导如何AC
[LeetCode] Word Break II (TLE)的更多相关文章
- 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 @ Python
原题地址:https://oj.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 [140]
[题目] Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where ...
- [Leetcode Week9]Word Break II
Word Break II 题解 题目来源:https://leetcode.com/problems/word-break-ii/description/ Description Given a n ...
随机推荐
- Python 学习笔记三
笔记三:函数 笔记二已取消置顶链接地址:http://www.cnblogs.com/dzzy/p/5289186.html 函数的作用: 給代码段命名,就像变量給数字命名一样 可以接收参数,像arg ...
- SPOJ 1811 Longest Common Substring
Description 给出两个字符串,求最长公共子串. Sol SAM. 这题随便做啊...后缀数组/Hash+二分都可以. SAM就是模板啊...直接在SAM上跑就行,没有了 \(go[w]\) ...
- linux awk, xargs
awk , 很赞的教程:http://coolshell.cn/articles/9070.html xargs, http://blog.csdn.net/andy572633/article/de ...
- wav转aac
//调用neroAacEnc.exe STARTUPINFO si={}; PROCESS_INFORMATION pi={};//隐藏窗口 si.cb=sizeof(si); si.dwFlags= ...
- vtigerCRM5.4的安装和汉化 ubuntu
由于php5.5经过较大改变,安装vtigercrm的时候可能需要修改一些配置 1.去官网下载vtiger5.4压缩包,解压后放在/var/www目录下 2.简单起见,将目录权限设置为777 vtig ...
- 什么情况下可以不写PHP的结束标签“?>”
我们经常看到有些PHP文件中的代码是只有开始标签,而没有结束标签的,那么什么情况下可以不写这个结束标签,而什么情况下必须写?先来看2个例子: 下面的代码正常运行: <?php echo 1234 ...
- oracle数据库高级应用之《触发器的建立》
(一)oracle数据库触发器的建立 eg1 CREATE OR REPLACE TRIGGER TRIGGER_ON_TD_DEPARTMENT AFTER INSERT OR UPDATE OR ...
- Binary Tree Upside Down
Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node that ...
- linux pep8 检查工具
感谢dongweiming大神.
- Appium根据xpath获取控件
如文章< Appium基于安卓的各种FindElement的控件定位方法实践>所述,Appium拥有众多获取控件的方法.其中一种就是根据控件所在页面的XPATH来定位控件. 本文就是尝试通 ...