首页 > 其他 > 详细

leetcode Word Break II

时间:2014-08-03 15:21:27      阅读:425      评论:0      收藏:0      [点我收藏+]

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;
  }
};




leetcode Word Break II,布布扣,bubuko.com

leetcode Word Break II

原文:http://blog.csdn.net/taoqick/article/details/38357329

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!