【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 ...
随机推荐
- C# DateTime格式化
DateTime. ToString() -- :: ToBinary() - ToFileTime() ToFileTimeUtc() ToLocalTime() -- :: ToLongDateS ...
- Objective-C访问SQLite
数据库的相关知识我就不去说明了,毕竟只要会sql语言的人就大家都一样. 本案例是在Xcode7.2环境下创建的single view application进行演示操作. 首先第一点,为什么要使用sq ...
- C# 微信扫码支付 回调页面
.NET版 微信扫码支付,官方推荐使用[模式二] 一.微信扫码支付模式一: 1.回调页面:官方demo中example文件下的NativeNotifyPage.aspx 2.微信回调地址:http:/ ...
- header函数
header函数 主要用于对http协议头设置相关信息 设置浏览器显示编码(解决乱码) header("Content-type:text/html;charset=utf-8") ...
- linux电源管理系列(一)
本系列将逐步介绍linux电源管理相关的知识,涉及到常见电源管理机制.linux电源管理机制.linux驱动中有关电源管理的相关接口.内核文档中关于Linux电源管理架构文档的分析.以下将以此来介绍相 ...
- 创建型模式——Builder
1.意图 将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示. 2.结构 3.参与者 Builder为创建一个Product对象的各个部件指定抽象接口 ConcreteBuild ...
- ubuntu 14.04 nagios4+ndoutils2.0+centreon2.5.4配置
ubuntu 14.04 nagios4+ndoutils2.0+centreon2.5.4(原创) 开发应用centreon是开源的IT监控软件,由法国人于2003年开发,最初名为Oreon,并于2 ...
- ECshop sina
http://blog.sina.com.cn/s/blog_5327408801019ofa.html http://blog.sina.com.cn/u/2624360105 http://blo ...
- Beaglebone Back学习四(GPIO实验)
GPIO Beaglebone Back开发板引出了92个引脚,其中只有65个GPIO口可通过配置使用,由于引脚具有“复用”的特性,大约每个引脚有8种工作模式(Beagle System Refere ...
- 【转】FTP自动上传文件的perl脚本以及配置文件
这个perl文件将执行的任务是, 按照指定的文件夹目录,自动将该文件夹下的所有文件上传到指定ftp站点的指定目录下 本应用程序设计的几个基本理念是:工具箱再利用:尽可能利用已有的工具:简化运行步骤:不 ...