首页 > 学院 > 开发设计 > 正文

140. Word Break II

2019-11-08 03:20:47
字体:
来源:转载
供稿:网友

很难的一道题2 刷研究吧,别人的代码

class Solution {public: unordered_map<string, vector<string>> m; vector<string> combine(string Word, vector<string> PRev){ for(int i=0;i<prev.size();++i){ prev[i]+=" "+word; } return prev; } vector<string> wordBreak(string s, unordered_set<string>& wordDict) { if(m.count(s)) return m[s]; vector<string> result; if(wordDict.count(s)){ result.push_back(s); } for(int i=1;i<s.size();++i){ string word=s.substr(i); if(wordDict.count(word)){ string rem=s.substr(0,i); vector<string> prev=combine(word,wordBreak(rem,wordDict)); result.insert(result.end(),prev.begin(), prev.end()); } } m[s]=result; return result; }};
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表