Leetcode -- Word Break
2015-10-28 14:47
309 查看
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 =
dict =
Return true because
For example, given
s =
"leetcode",
dict =
["leet", "code"].
Return true because
"leetcode"can be segmented as
"leet code".
class Solution { public: bool wordBreak(string s, unordered_set<string>& wordDict) { int n=s.size(); if(n==0) return 0; if(wordDict.count(s)>0) return 1; vector<bool> f(n,0); string cur,tmp; for(int i=n-1;i>=0;--i) { cur = s.substr(i); if(wordDict.count(cur)>0) f[i]=1; else { for(int j=i+1;j<n;++j) { tmp = s.substr(i,j-i); if(wordDict.count(tmp)>0&&f[j]) { f[i]=1; break; } } } } return f[0]; } };
相关文章推荐
- 浅谈Swift集合类型
- OC学习笔记(六)
- 混淆后显示行号
- HTML meta viewport属性
- 翻译一篇文章:It's Difficult to Grow a Test Developer(成为测试开发工程师的艰辛)
- eclipse4 以后,RCP 界面开发configurer.setShellStyle(SWT.MIN | SWT.CLOSE);不生效的解决方法
- git diff ^M 每个项目配置行结束符
- TCP洪水攻击(SYN Flood)的诊断和处理
- iOS9使用提示框的正确实现方式
- 读书计划清单
- POST上传文件的原理剖析以及实例验证
- 辗转相除法(欧几里得算法)
- 让我们聊聊秒杀这东西
- OC学习笔记(五)
- Xcode清楚缓存、清理多余证书
- 七个对我最重要的职业建议(译文)
- LeetCode 112: Path Sum
- 深入理解java异常处理机制
- strcpy,strcat,strcmp的函数实现
- iOS学习笔记37-iOS9适配 之 关于info.plist 第三方登录 添加URL Schemes白名单