[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 ...
随机推荐
- 浅析mongoEngine的document对象
引言: from mongoengine import * connect('local')class Test(Document): name=StringField(max_length=32) ...
- 页面遮罩层,并且阻止页面body滚动。bootstrap模态框原理
实现思路: 1.需要有一个层将body遮住,放在body上方. 2.修改body的overflow属性值为:hidden 废话不多说了,将关键代码贴出来了,兼容火狐,谷歌,ie 遮罩层的样式代码,红色 ...
- Nagios+zabbix+ganglia的相关参数分析和优缺点介绍
转自: http://blog.csdn.net/messiaDemo/article/details/52046822?utm_source=itdadao&utm_medium=refer ...
- Python之调用WebService:suds
suds官方: https://fedorahosted.org/suds/wiki/Documentation 互联网公开WSDL: http://www.webxml.com.cn/zh_cn/w ...
- gcc-5.4.0 static dwarf2 compile
------------------------------------------------------------------------------- 又开始折腾了, 静态编译 gcc-5.4 ...
- Ubuntu ( Linux) Eclipse 乱码问题
刚装完Ubuntu,导入Java和Android项目时,发现字符乱码,究其原因,是由于Windows下使用的是GBK编码,而Ubuntu使用的是UTF-8编码.网上查找了相关资料,主要解决方案有两种. ...
- 一道常考fork题挖掘
#include <stdio.h> #include <sys/types.h> #include <unistd.h> int main(void) { int ...
- RecyclerView的万能分割线
效果图: 使用方法: 添加默认分割线:高度为2px,颜色为灰色 mRecyclerView.addItemDecoration(new RecyclerViewDivider(mContext, Li ...
- 【leetcode】 Palindrome Partitioniong (middle) (*^__^*)
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- 【leetcode】Find Minimum in Rotated Sorted Array I & II (middle)
1. 无重复 Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 ...