[leetcode]Word Break
来源:互联网 发布:jquery post json 编辑:程序博客网 时间:2024/05/17 00:50
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"
.
方法有参考:动态规划
class Solution {public: //动态规划的问题最后要从后向前去做,简单些 bool wordBreak(string s, unordered_set<string> &dict) { size_t n = s.size(); if(n == 0) return true; vector<bool> vb(n + 1, false); vb[n] = true; for(int i = n - 1; i >= 0; i--){ for(unordered_set<string>::iterator itor = dict.begin(); itor != dict.end(); itor++){ int dwordlen = itor->size(); //字典中单词的长度 int tlen = n - i; //当前要查找的字符串位置 到末尾的距离 if(tlen >= dwordlen && !vb[i]){ //一定要保证在i + dwordlen之前的字符串可以查找到 if(vb[i + dwordlen] && s.substr(i, dwordlen) == *itor){ vb[i] = true; break; } } } } return vb[0]; }};
0 0
- leetcode Word Break & Word Break ||
- Leetcode: Word Break
- [leetcode]Word Break
- [leetcode]Word Break II
- LeetCode:Word Break
- LeetCode:Word Break II
- Leetcode: Word Break II
- [LeetCode] Word Break
- [LeetCode] Word Break II
- LeetCode: Word Break
- leetcode之Word Break
- [LeetCode]Word Break II
- [LeetCode]Word Break
- leetcode-Word Break
- LeetCode 之 Word Break
- leetcode word break
- LeetCode | Word Break
- LeetCode | Word Break II
- Hadoop如何处理关联计算
- php绘图的方法
- Partitioner, SortComparator and GroupingComparator in Hadoop
- JSP_<jsp:application>实例
- cmd路径里含有空格的处理
- [leetcode]Word Break
- android NFC学习笔记
- shell中,echo的-n和-e参数含义
- 支付宝 Android SDK 吐槽+辟谣(一)
- c
- [LeetCode]4Sum
- STL之:简单的vector实现
- python3自动爬笑话
- 为工程系统增加人员界面