【LeetCode】139 - Word Break
2015-08-11 23:38
453 查看
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
Solution:
dppos[i]==true/false表示字符串从开头到i的子串是否存在cut方案满足条件
动态规划设置初值bpos[0]==true
string.substr(int beginIndex, int length): 取string从beginIndex开始长length的子串
For example, given
s =
"leetcode",
dict =
["leet", "code"].
Return true because
"leetcode"can be segmented as
"leet code".
Solution:
dppos[i]==true/false表示字符串从开头到i的子串是否存在cut方案满足条件
动态规划设置初值bpos[0]==true
string.substr(int beginIndex, int length): 取string从beginIndex开始长length的子串
class Solution { public: bool wordBreak(string s, unordered_set<string>& wordDict) { //runtime:4ms vector<bool> dppos(s.size()+1, false); dppos[0]=true; for(int i=1;i<dppos.size();i++){ for(int j=i-1;j>=0;j--){ //从右到左找快很多 if(dppos[j]==true && wordDict.find(s.substr(j,i-j))!=wordDict.end()){ dppos[i]=true; break; //只要找到一种切分方式就说明长度为i的单词可以成功切分,因此可以跳出内层循环 } } } return dppos[s.size()]; } };
相关文章推荐
- iOS 内存管理总结
- Android Studio 工具栏切换卡顿
- java一些常见常识
- 黑马程序员——Java基础---GUI,网络编程
- hdoj-1233 还是畅通工程【最小生成树--kruskal】
- JS跟随鼠标旋转的图片
- Redis安装-ubuntu
- 编程规范(一 之kmalloc,fflush,fclose,char_init)
- 马和瓦的问题
- 【转】HBase基本原理
- HDU 5353(Average-贪心分果)
- 黑马程序员--java基础学习笔记8
- ios bash 使用记录
- 修改Android签名证书keystore的密码、别名alias以及别名密码
- 理解javascript中的策略模式
- java 重写的学习
- HDUOJ-1879(继续畅通工程)(最小生成树)
- android那些事(一)
- NTT 分类: templates 2015-...
- NTT