class Solution { public: vector<string> removeComments(vector<string>& source) { vector<string> res; string ln; int state = 0; for (const auto & line : source) { for (int i = 0, ll = line.length(); i < ll; i++) { if (state == 0) { if (i < ll-1) { if (line[i] == ‘/‘ && line[i+1] == ‘/‘) break; // // comment, skip line else if (line[i] == ‘/‘ && line[i+1] == ‘*‘) { state = 1; i += 1; continue; } } ln.push_back(line[i]); } else if (state == 1) { // inside /* if (i < ll-1 && line[i] == ‘*‘ && line[i+1] == ‘/‘) { state = 0; i += 1; continue; } } } if (state == 0 && ln.length() > 0) { res.push_back(ln); ln = ""; } } if (ln.length() > 0) res.push_back(ln); return res; } };
原文地址:https://www.cnblogs.com/JTechRoad/p/9986719.html
时间: 2024-11-05 19:40:02