【leetcode】word-break-ii
来源:互联网 发布:数据分析师需要考证吗 编辑:程序博客网 时间:2024/06/05 08:57
题目:
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"].
代码分析:
void _wordBreak(vector<string> &ret,string str,//表示的是要操作的字符串string &src,//表示的题目给的字符串int idex,///当前的字符串走到哪个位置unordered_set<string> &dict,set<int>& size){//表示已经走完了if(idex == src.size()){if(str.size() != 0)ret.push_back(str);return ;}//要是当前的字符串为不为空的话,那么在尾部加上空格 if(idex != 0)str.push_back(' ');set<int>::iterator len = size.begin();for(; len!= size.end();++len){int i= *len;if(idex + i > src.size()||i==0)break;string tmp((char*)src.c_str()+idex,(char*)src.c_str()+idex+i);unordered_set<string>::iterator it = dict.find(tmp); if(it != dict.end())_wordBreak(ret,str+tmp,src,idex+i,dict,size);}}vector<string> wordBreak(string s, unordered_set<string> &dict) {vector<string> ret;if(s.size() == 0||dict.empty() == true)return ret;unordered_set<string>::iterator it = dict.begin();set<int> size;while(it!= dict.end()){size.insert(it->size());++it;}string str;_wordBreak(ret,str,s,0,dict,size);return ret;}
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
- 周志华《机器学习》课后习题解答系列(六):Ch5.7
- 基础算法(二)---数据结构之图
- java中讲讲BufferedInputStream的用法,举例?
- Android原生分享功能的思考与实现
- Servlet返回中文
- 【leetcode】word-break-ii
- HDU1429 胜利大逃亡(续)(状态压缩+广搜BFS )
- 《C语言及程序设计》实践项目——动态数组 ->【项目2-动态数组排序】
- 动态规划——背包问题(一)
- loss function 的简单理解
- PAT 1002 乙等 (写出这个数)c++
- Pytorch学习入门(二)--- Autograd
- 深度学习之AlexNet解读
- ERROE