【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 ...
随机推荐
- 修改Windows硬盘分区名称
本文由 www.169it.com 收集整理 如果用户在将 XP 重装成Win7/Win8时,原本的硬盘分区名称可能会出现无法更改的情况,重新命名也都起不了作用.这种情况一般是因为使用 XP 系统下 ...
- js 比较两个日期的大小的例子
例子,直接比较大小即可 代码如下 复制代码 <script>var st="2009-10-20 14:38:40"var et="2009-10-20 15 ...
- 第九篇、Swift的基本使用
1.访问权限 /* 1> internal : 内部的 1. 默认情况下所有的类&属性&方法的访问权限都是internal 2. 在本模块(项目/包/target)中可以访问 2 ...
- OC7_单词个数
// // WordManger.h // OC7_单词个数 // // Created by zhangxueming on 15/6/17. // Copyright (c) 2015年 zhan ...
- (转)使用Visual Studio 2015开发Android 程序
环境配置: 操作系统:win 7 64位 IDE:Visual Studio 2015 SDK:installer_r24.3.3-windows 安装前提: 编辑hosts文件(在附件可下载)因为安 ...
- 问题解决:引入com.sun.management.OperatingSystemMXBean 出错
不能import com.sun.management.OperatingSystemMXBean 解决: Eclipse默认把这些受访问限制的API设成了ERROR.只要把Windows-Pref ...
- Bitmap、CBitmap、HBITMAP以及BITMAP的相互转换
Bitmap.CBitmap.HBITMAP以及BITMAP的相互转换 构建CBitmapCBitmap bmp;bmp.LoadBitmap(ID); 构建HBITMAPHBitmap = (HBI ...
- Visual Studio通过Web Deploy发布网站报错:An error occurred when the request was processed on the remote computer.
这个问题很奇怪,不管我怎么重启服务器和自己的开发机,都没有用. 在网上找了很多资料,有说可以尝试去读Windows的错误日志,然后通过日志找原因…(详见Stackoverflow:http://sta ...
- 《C++ Qt 设计模式》8|15拼图 小游戏的简单实现。拜托,别乱点!
第零章:介绍 看到这个游戏了,感觉蛮好玩的,实现了一下. 界面如下: 游戏玩法:在3×*3的矩阵中,每个按钮都可以点击,如果按钮四周有一个是空白,则点击此按钮则会移动到这个空白.按钮字母顺序变成“AB ...
- NGINX+UWSGI 莫名发生Nginx 502 Bad Gateway错误的排查过程
自己有个阿里云UBUNTU运行的Django站,使用NGINX+UWSGI驱动,今天登陆系统后台更新内容出现了几个大字:Nginx 502 Bad Gateway,一看情况不好,这是要糟糕啊. 啊西八 ...