LeetCode 139. Word Break
2016-07-03 19:17
495 查看
Problem: https://leetcode.com/problems/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 =
dict =
Return true because
Thought:
for each position, traverse whether it is end with word in wordDict, if is ,and check position[i - len] is true. then position[i] = true
the result depend on position[s.length]
Code C++:
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".
Thought:
for each position, traverse whether it is end with word in wordDict, if is ,and check position[i - len] is true. then position[i] = true
the result depend on position[s.length]
Code C++:
class Solution { public: bool wordBreak(string s, unordered_set<string>& wordDict) { if (s.length() <= 0 || wordDict.size() <= 0) { return false; } vector<int> check(s.length() + 1, 0); check[0] = 1; for (int i = 1; i <= s.length(); i++) { for (const string& word : wordDict) { int len = word.length(); if (i - len >= 0 && s.substr(i-len,len) == word && check[i - len] == 1) { check[i] = 1; } } } return check[s.length()] == 1; } };
相关文章推荐
- Atom设置震撼的编辑效果
- 李洪强iOS开发之下载
- XBlock 学习一
- Nginx中文域名配置
- 英文名句、哲理、歌词
- 括号匹配(二)
- 网站的架构演进
- 去掉eclipse的拼写检查
- Java中try catch finally语句中含有return语句的执行情况(总结版)
- [vim]插件ctrlp
- RPN delphi
- OAuth2.0认证和授权原理
- 正则表达式
- 全国省市区数据库,带拼音,简称,行政编码,邮政编码等
- 分区与分桶的比较
- Xcode instruments 之CPU
- debug.keystore
- 如何在ViewPager外部获取“当前显示的页面的位置position”
- ListView的使用
- 【Codeforces】-#222A-KShooshuns and Sequence(思维)