[leetcode] Word Break II
来源:互联网 发布:js数组转为json字符串 编辑:程序博客网 时间:2024/06/07 20:43
Word Break II
class Solution {public: vector<string> wordBreak(string s, unordered_set<string> &dict) { vector<bool> fun(s.size()+1,false);// 长度为 n 的字符串有 n+1 个隔板 // prev[i][j] 为 true,表示 s[j, i) 是一个合法单词,可以从 j 处切开 // 第一行未用 vector<vector<bool>> prev(s.size()+1,vector<bool>(s.size())); fun[0]=true;//状态初始化 for(int i=1;i<=s.size();++i){ for(int j=i-1;j>=0;--j){ if(fun[j]&&dict.find(s.substr(j,i-j))!=dict.end()){ fun[i]=true; prev[i][j]=true; } } } vector<string> res; vector<string> path; gen_path(s,prev,s.size(),path,res); return res; } //DFS遍历,生成路径 void gen_path(const string &s,const vector<vector<bool>> &prev,int cur,vector<string> &path,vector<string> &res){ if(cur==0){ string tmp; for(auto iter=path.crbegin();iter!=path.crend();++iter){//逆序遍历 tmp+=*iter+" "; } tmp.erase(tmp.end()-1);//删除最后一个空格 res.push_back(tmp); } for(size_t i=0;i<s.size();++i){ if(prev[cur][i]){ path.push_back(s.substr(i,cur-i));//分割 gen_path(s,prev,i,path,res);//递归 path.pop_back(); } } } };
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
- systemverilog-modelsim中运行命令,时间精度,include
- OpenGL: 屏幕二维坐标转化成三维模型坐标
- linux 学习书籍
- 的发生的方式法规的规定发鬼地方
- 今日APPS:手机戒瘾法宝Forest 拒当低头族
- [leetcode] Word Break II
- java数据库--JDBC
- WP开发环境配置过程中遇到的问题总结
- HDU4882
- How to calculate ADF table column
- OpenGL: 屏幕坐标向OpenGL坐标转换
- UVA - 825Walking on the Safe Side(dp)
- 对FlexLite和MoreUI的评价
- 段永基说四通公司的失败(上)