首页 > 代码库 > leetcode Word Break II
leetcode Word Break II
Give two versions of WA code:
Version 1: Runtime Error: Find the bug
class Solution { public: void find(const string& s, const unordered_set<string>& dict, vector<int>& res) { int i, j, len = s.length(); for (i = 0; i < len; ++i) { if (dict.find(s.substr(0, i + 1)) != dict.end()) { res.push_back(i); } } } void addSpace(vector<string>& cur, vector<vector<string> >& resvec, const string& s, const unordered_set<string>& dict) { if (s.length() == 0) { resvec.push_back(cur); return; } vector<int> breakpoints; find(s, dict, breakpoints); for (int i = 0; i < breakpoints.size(); ++i) { cur.push_back(s.substr(0, breakpoints[i] + 1)); addSpace(cur, resvec, s.substr(breakpoints[i]), dict); cur.pop_back(); } } vector<string> wordBreak(string s, unordered_set<string> &dict) { int i, j, size; vector<vector<string> > resvec; vector<string> res, cur; addSpace(cur, resvec, s, dict); for (i = 0; i < resvec.size(); ++i) { string tmp = resvec[i][0]; for (j = 1; j < resvec[i].size(); ++j) tmp += " " + resvec[i][j]; res.push_back(tmp); } return res; } };
Version 2: TLE
class Solution { public: void find(const string& s, const unordered_set<string>& dict, vector<int>& res) { int i, j, len = s.length(); for (i = 0; i < len; ++i) { if (dict.find(s.substr(0, i + 1)) != dict.end()) { res.push_back(i); } } } void addSpace(vector<string>& cur, vector<vector<string> >& resvec, const string& s, const unordered_set<string>& dict) { if (s.length() == 0) { resvec.push_back(cur); return; } vector<int> breakpoints; find(s, dict, breakpoints); for (int i = 0; i < breakpoints.size(); ++i) { cur.push_back(s.substr(0, breakpoints[i] + 1)); addSpace(cur, resvec, s.substr(breakpoints[i] + 1), dict); cur.pop_back(); } } vector<string> wordBreak(string s, unordered_set<string> &dict) { int i, j, size; vector<vector<string> > resvec; vector<string> res, cur; addSpace(cur, resvec, s, dict); for (i = 0; i < resvec.size(); ++i) { string tmp = resvec[i][0]; for (j = 1; j < resvec[i].size(); ++j) tmp += " " + resvec[i][j]; res.push_back(tmp); } return res; } };Version 3: AC Code:
class Solution { public: void find(const string& s, const unordered_set<string>& dict, vector<int>& res) { int i, j, len = s.length(); for (i = 0; i < len; ++i) { if (dict.find(s.substr(0, i + 1)) != dict.end()) { res.push_back(i); } } } void addSpace(vector<string>& cur, vector<vector<string> >& resvec, const string& s, const unordered_set<string>& dict, vector<int>& forbidden) { if (s.length() == 0) { resvec.push_back(cur); return; } if (forbidden[s.length()]) return; vector<int> breakpoints; find(s, dict, breakpoints); for (int i = 0; i < breakpoints.size(); ++i) { cur.push_back(s.substr(0, breakpoints[i] + 1)); int resvecsize = resvec.size(); addSpace(cur, resvec, s.substr(breakpoints[i] + 1), dict, forbidden); if (resvec.size() == resvecsize) forbidden[s.length()-breakpoints[i]-1] = 1; cur.pop_back(); } } vector<string> wordBreak(string s, unordered_set<string> &dict) { int i, j, size; vector<vector<string> > resvec; vector<string> res, cur; vector<int> forbidden(s.length()+1, 0); addSpace(cur, resvec, s, dict, forbidden); for (i = 0; i < resvec.size(); ++i) { string tmp = resvec[i][0]; for (j = 1; j < resvec[i].size(); ++j) tmp += " " + resvec[i][j]; res.push_back(tmp); } return res; } };
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。