[LeetCode] Word Break II
来源:互联网 发布:人工智能的前景 编辑:程序博客网 时间:2024/05/29 14:21
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word.
Return all such possible sentences.
For example, given
s = "catsanddog"
,
dict = ["cat", "cats", "and", "sand", "dog"]
.
A solution is ["cats and dog", "cat sand dog"]
.
class Solution {public: vector<string> wordBreak(string s, unordered_set<string> &dict) { int len = s.size(); vector<vector<bool>> flag(len, vector<bool>(len, false)); for(int i = 0; i < len; i++) { for(int j = 0; j < len; j++) { if(dict.find(s.substr(i,j-i+1)) != dict.end()) flag[i][j] = true; } } vector<string> result; for(int i = len-1; i >= 0; i--) { if(flag[i][len-1]) { string str = s.substr(i, len-i); back(flag, i-1, s, str, result); } } return result; } void back(vector<vector<bool>> &flag, const int &i, const string &s, string str, vector<string> &result) { if(i==-1) { result.push_back(str); return; } for(int j = 0; j <= i; j++) { if(flag[j][i]) { string cur_str = s.substr(j, i-j+1) + " " + str; back(flag, j-1, s, cur_str, result); } } }};
0 0
- [leetcode]Word Break II
- LeetCode:Word Break II
- Leetcode: Word Break II
- [LeetCode] Word Break II
- [LeetCode]Word Break II
- LeetCode | Word Break II
- [LeetCode] - Word Break II
- LeetCode - Word Break II
- Leetcode Word Break II
- [LeetCode] Word Break II
- Word Break II -- LeetCode
- LeetCode (Word Break II )
- Leetcode--Word Break II
- Leetcode: Word Break II
- LeetCode Word Break II
- Word Break II leetcode
- [leetcode]Word Break II
- leetcode -- word break II
- 怎么把JPG格式的图片转换成PDF
- 详解协方差与协方差矩阵
- 计算机IT求职学习List
- Linux Shell 文本处理工具集锦
- 《将博客搬至CSDN》
- [LeetCode] Word Break II
- android 中一个工程引用另一个工程
- iOS Icon and Image Sizes
- 王立平--JAVA SE,JAVA EE,JAVA ME区别
- 标志位和中断位的区别:USART_ClearFlag和USART_ClearITPendingBit
- ios 终于可以在html5的video控件中自动播放了
- 如何使用TestFlight进行Beta测试
- android的listview中setselection()不起作用的解决方案
- Android开发自定义View之滑动按钮与自定义属性