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 ...
随机推荐
- AndroidTouch事件总结
1.自定义的控件几乎都要用到触摸事件,不交互怎么响应,相关的事件处理函数由dispatchTouchEvent.onInterceptTouchEvent.onTouchEvent,处理这些事件的由v ...
- windows C input 注意
windows控制台输入,默认是以文本模式打开,即使重定向输入,文本模式不变,所以输入时无法读到cr,因为windows已经把crlf转换成单个lf. 如果freopen("CON" ...
- 学习winform第三方界面weiFenLuo.winFormsUI.Docking.dll
控件dockpanel中提供了几个可用的类, 重要的有两个, 一是DockPanel, 一是DockContent, DockPanel是从panel继承出来的, 用于提供可浮动的dock的子窗口进行 ...
- Android FragmentTransactionExtended:使Fragment以多种样式动画切换
有多种fragment之间切换的效果,效果是这样的: Demo的实现是很简单的. 在res/values中,新建一个arrays.xml文件,存放Fragment动画效果的名称,在spinner中使用 ...
- Android Token的使用学习
学习Token Token是什么? Token是服务端生成的一串字符串,以作客户端进行请求的一个令牌,当第一次登录后,服务器生成一个Token便将此Token返回给客户端,以后客户端只需带上这个Tok ...
- Python中的下划线(译文)
原文地址这篇文章讨论Python中下划线_的使用.跟Python中很多用法类似,下划线_的不同用法绝大部分(不全是)都是一种惯例约定. 单个下划线(_) 主要有三种情况: 1. 解释器中 _符号是指交 ...
- Buffer、Channel示例
a.txt 孔雀向西飞,今朝更好看.孔雀向西飞,今朝更好看.孔雀向西飞,今朝更好看.孔雀向西飞,今朝更好看. 示例一. package com.test; import java.io.FileI ...
- 010-Scala单例对象、伴生对象实战详解
010-Scala单例对象.伴生对象实战详解 Scala单例对象详解 函数的最后一行是返回值 子项目 Scala伴生对象代码实战 object对象的私有成员可以直接被class伴生类访问,但是不可以被 ...
- oracle 归档日志
归档日志(Archive Log)是非活动的重做日志备份.通过使用归档日志,可以保留所有重做历史记录,当数据库处于ARCHIVELOG模式并进行日志切换式,后台进程ARCH会将重做日志的内容保存到归档 ...
- FB
转眼间,开始工作到现在好几年,忙着功能,忙着补漏填坑,忙着项目,现在回顾着开始的理想,一时有点恍惚,然后鄙视了下自己居然还在“理想”中…… 那就开始吧,做点什么呢? DX9/DX11的支持是必须的,S ...