leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

0722.cpp (1008B)


      1 class Solution {
      2   public:
      3     vector<string> removeComments(vector<string> &source) {
      4         vector<string> res = {""};
      5         bool block = false;
      6 
      7         for (const auto &l : source) {
      8             int j = 0, i;
      9 
     10             for (i = 0; i < size(l); i++) {
     11                 if (!block && l[i] == '/' && l[i + 1] == '/')
     12                     break;
     13                 else if (!block && l[i] == '/' && l[i + 1] == '*') {
     14                     if (i != j) res.back() += l.substr(j, i - j);
     15 
     16                     i++; // prevent /*/ as close
     17                     block = true;
     18                 } else if (block && l[i] == '*' && l[i + 1] == '/') {
     19                     j = i + 2;
     20                     i++; // prevent */* as open
     21                     block = false;
     22                 }
     23             }
     24 
     25             if (!block && i != j) res.back() += l.substr(j, i - j);
     26             if (!block && !res.back().empty()) res.push_back("");
     27         }
     28 
     29         if (res.back().empty()) res.pop_back();
     30         return res;
     31     }
     32 };