【leetcode】Word Break II (hard)★
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"].
思路:
我自己用回溯做超时了,直接看大神的20ms代码吧
主要是即考虑了从前向后的连接,也考虑了从后向前的连接。
class Solution {
public:
vector<string> wordBreak(string s, unordered_set<string> &dict)
{
vector<vector<int>> flag(s.size() + , vector<int>());
flag[].push_back();
for (int i = ; i <= s.size(); i++) //当前判断字符串的结束位置的下一个位置 如i=1 表示结束位置是 s[0] 从前向后
{
for (int j = ; j < i; j++) //当前判断的字符串的起始位置
{
if (!flag[j].empty() && dict.find(s.substr(j, i - j)) != dict.end()) //只有该单词前面的单词能够找到时才压入结果
{
flag[i].push_back(j); //flag[i]中存储 以i为结束位置的下一个位置的单词 的起始位置 如flag[2] 里面存储的都是以s[1]结束的单词的第一个字母的位置
}
}
}
vector<string> result;
getResult(result, flag, s, s.size()); //从后向前找结果, 只有能够划分到最后一个字母的单词切割方式才考虑
return result;
}
void getResult(vector<string> &result, vector<vector<int>> &flag, string s, int n)
{
for (int j = ; j < flag[n].size(); j++)
{
int i = flag[n][j];
if (i == ) //找到起始点了,压入划分的答案
{
result.push_back(s);
continue;
}
s.insert(s.begin() + i, ' ');
getResult(result, flag, s, i);
s.erase(i, );
}
}
};
我自己TLE的代码,我只考虑了从前向后,非常繁琐。但具体为什么会慢那么多我还没想明白。仅仅是因为没有考虑从后向前吗?
class Solution {
public:
vector<string> wordBreak(string s, unordered_set<string> &dict) {
vector<vector<bool>> issubstr(s.size(), vector<bool>(s.size(), false));
for(int i = ; i < s.size(); i++)
{
for(int j = ; j <= s.length() - i; j++)
{
if((find(dict.begin(), dict.end(), s.substr(i, j))) != dict.end())
{
issubstr[i][i + j - ] = true;
}
}
}
vector<string> ans;
vector<string> X;
vector<vector<string>> S();
int k = ;
for(int i = ; i <= s.length(); i++)
{
if(issubstr[][i - ])
{
S[k].push_back(s.substr(, i));
}
}
while(k >= )
{
while(!S[k].empty())
{
while(X.size() > k)
{
X.pop_back();
}
X.push_back(S[k].back());
S[k].pop_back();
int Xtotallen = ;
vector<string>::iterator it;
for(it = X.begin(); it != X.end(); it++)
{
Xtotallen += it->length();
}
if(Xtotallen == s.length())
{
string partans = X[];
for(it = X.begin() + ; it != X.end(); it++)
{
partans += " ";
partans += (*it);
}
ans.push_back(partans);
}
else
{
k++;
if(S.size() <= k)
{
S.push_back(vector<string>());
}
for(int i = ; i <= s.length() - Xtotallen; i++)
{
if(issubstr[Xtotallen][Xtotallen + i -])
{
S[k].push_back(s.substr(Xtotallen, i));
}
}
}
}
k--;
}
return ans;
}
};
【leetcode】Word Break II (hard)★的更多相关文章
- 【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 (middle)
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
- 【LeetCode】Word Break 解题报告
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
- 【leetcode】Word Ladder II
Word Ladder II Given two words (start and end), and a dictionary, find all shortest transformation ...
- 【leetcode】Word Break
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
- 【leetcode】Word Break(python)
思路是这种.我们从第一个字符開始向后依次找,直到找到一个断句的地方,使得当前获得的子串在dict中,若找到最后都没找到.那么就是False了. 在找到第一个后,接下来找下一个断句处,当然是从第一个断句 ...
- 【leetcode】Word Search II(hard)★
Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...
- 【leetcode】Word Ladder II(hard)★ 图 回头看
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...
- [Leetcode Week9]Word Break II
Word Break II 题解 题目来源:https://leetcode.com/problems/word-break-ii/description/ Description Given a n ...
随机推荐
- DOS批处理命令-call命令
call命令 在批处理中调用别的批处理或者可运行程序或者 バッチ プログラムを別のバッチ プログラムから呼び出します. 语法 1.CALL [驱动盘符:][路径]文件名 [参数] 调用并执行[驱动盘符 ...
- (转)使用CruiseControl+SVN+ANT实现持续集成之三
在上一节中我们介绍了环境搭建和配置介绍,并快速启动CC查看集成结果,在本节中我们将详细介绍CC构建操作及监视. 1. 启动CC服务器 通过执行其根目录下的cruisecontrol.bat文件来启动C ...
- 免费的HTML5连载来了《HTML5网页开发实例详解》连载(五)图解通过Fiddler加速开发
Fiddler是Windows底下最强大的请求代理调试工具,监控任何浏览器的HTTP/HTTPS流量,窜改客户端请求和服务器响应,解密HTTPS Web会话,图4.44为Fiddler原理示意图. 图 ...
- Php 输出语句
输出语句 echo 示例: print(); 示例: 只能输出标量数据类型,无法输出布尔false print_r(); print_r可以以比较容易理解的方式显示数据 示例: var_dump(); ...
- 关键字 virtual
Virtual是C++ OO机制中很重要的一个关键字.只要是学过C++的人都知道在类Base中加了Virtual关键字的函数就是虚拟函数(例如函数print),于是在Base的派生类Derived中就 ...
- HTML5之图形变换
- Transformations scale(0.5,0.5) 缩放 rotate(0.175) 旋转 translate(100,50) 位移 - 代码结构 context.scale(x, y) ...
- 真正的inotify+rsync实时同步 彻底告别同步慢
真正的inotify+rsync实时同步 彻底告别同步慢 http://www.ttlsa.com/web/let-infotify-rsync-fast/ 背景 我们公司在用in ...
- hadoop下跑mapreduce程序报错
mapreduce真的是门学问,遇到的问题逼着我把它从MRv1摸索到MRv2,从年前就牵挂在心里,连过年回家的旅途上都是心情凝重,今天终于在eclipse控制台看到了job completed suc ...
- sql 查询练习
1. 用一条SQL 语句 查询出每门课都大于80 分的学生姓名name kecheng fenshu 张三 语文 81张三 数学 75李四 语文 76李四 数学 90王五 语文 81王五 数学 100 ...
- CURL的使用<发送与接收数据>
$headers = array( "TYPE:xxxxooooo", "TOKEN:00000000" ); $data = array( 'data' =& ...