LeetCode-Solutions-in-Cpp17

Solutions to high-frequency interview questions of LeetCode in C++17, taking into account both efficiency and comprehensibility.


Project maintained by downdemo Hosted on GitHub Pages — Theme by mattgraham
class Solution {
 public:
  vector<string> removeComments(vector<string>& source) {
    bool in_comment = false;
    vector<string> res;
    string s;
    for (auto& x : source) {
      for (int i = 0; i < size(x); ++i) {
        string t{x.substr(i, 2)};
        if (in_comment) {
          if (t == "*/") {
            in_comment = false;
            ++i;
          }
          continue;
        }
        if (t == "/*") {
          ++i;
          in_comment = true;
          continue;
        }
        if (t == "//") {
          break;
        }
        s += x[i];
      }
      if (!in_comment && !empty(s)) {
        res.emplace_back(s);
        s.clear();
      }
    }
    return res;
  }
};