Word Break
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words.
For example, given
s = "leetcode",
dict = ["leet", "code"].
Return true because "leetcode" can be segmented as "leet code"
给一个字符串s和一个字典dict,编程实现s能否由dict中组合而成。
方案一的DFS提交TLE,方案二使用动态规划
class Solution{
private:
void helper(string s,unordered_set<string>& wordDict,int start,bool& res){
if(start == int(s.length())){
res = true;
return;
}
for(int i = start;i<int(s.size());i++){
string word = s.substr(start,i - start + );
if(wordDict.find(word) != wordDict.end() && !res){
helper(s,wordDict,i+,res);
}
}
}
public:
bool wordBreak(string s,unordered_set<string>& wordDict){
bool res = false;
helper(s,wordDict,,res);
return res;
}
};
class Solution2{
public:
bool wordBreak(string s,unordered_set<string>& wordDict){
vector<bool> res(s.size()+,false);
res[] = true;
for(int i=;i<int(s.size())+;i++){
for(int j=;j<i;j++){
if(res[j] && wordDict.find(s.substr(j,i-j)) != wordDict.end()){
res[i] = true;
break;
}
}
}
return res.back();
}
};
Word Break的更多相关文章
- [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
Word Break II Given a string s and a dictionary of words dict, add spaces in s to construct a senten ...
- 17. Word Break && Word Break II
Word Break Given a string s and a dictionary of words dict, determine if s can be segmented into a s ...
- LeetCode:Word Break II(DP)
题目地址:请戳我 这一题在leetcode前面一道题word break 的基础上用数组保存前驱路径,然后在前驱路径上用DFS可以构造所有解.但是要注意的是动态规划中要去掉前一道题的一些约束条件(具体 ...
- LeetCode Word Break II
原题链接在这里:https://leetcode.com/problems/word-break-ii/ 题目: Given a string s and a dictionary of words ...
- 【LeetCode OJ】Word Break II
Problem link: http://oj.leetcode.com/problems/word-break-ii/ This problem is some extension of the w ...
- Leetcode#139 Word Break
原题地址 与Word Break II(参见这篇文章)相比,只需要判断是否可行,不需要构造解,简单一些. 依然是动态规划. 代码: bool wordBreak(string s, unordered ...
- 【leetcode】Word Break (middle)
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
- 140. Word Break II
题目: Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where e ...
- 139. Word Break
题目: Given a string s and a dictionary of words dict, determine if s can be segmented into a space-se ...
随机推荐
- [转载] 百度上传&下载脚本
下载百度网盘中的文件,最简单的办法是使用wget命令.找到百度网盘文件,点击下载,然后找到相应的下载地址,复制链接地址即可.但这种方法有一个bug,因为百度云网盘访问文件的链接地址是动态的,当文件比较 ...
- [并查集] POJ 1182 食物链
食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 66294 Accepted: 19539 Description ...
- bzoj 3504: [Cqoi2014]危桥
#include<cstdio> #include<iostream> #include<cstring> #define M 100009 #define inf ...
- 美化iTerm2
一.下载iTerm2,启动 二.安装oh-my-zsh curl -L https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/insta ...
- Bootstrap <基础十九>分页
Bootstrap 支持的分页特性.分页(Pagination),是一种无序列表,Bootstrap 像处理其他界面元素一样处理分页. 分页(Pagination) 下表列出了 Bootstrap 提 ...
- Android 取源码时 error: could not verify the tag 'v1.12.4'的解决
原帖地址http://forum.xda-developers.com/showthread.php?p=46100064也没说啥原因 repo init -u git://github.com/Cy ...
- 《java集合概述》
JAVA集合概述: Collection: |---List有序的:通过索引就可以精确的操作集合中的元素.元素是可以重复的. List提供了增删改查的动作. 增加add(element) add(in ...
- 小型资源管理器,IO操作,读取和写入文件和目录的常用操作
解决方案: 小总结: 用IO流,的file,DirectoryInfo的方法绑定Treeview控件上和删除,读取, 可以熟练掌握一下IO流 主页面: private void Form1_Load( ...
- Asp.net attributes collection
<?xml version="1.0" encoding="utf-8"?><root> <ContralNames> ...
- iOS应用性能调优好文mark
http://www.cocoachina.com/ios/20150408/11501.html