-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremoveComments.cpp
More file actions
44 lines (43 loc) · 1.34 KB
/
removeComments.cpp
File metadata and controls
44 lines (43 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// Source: https://leetcode.com/problems/remove-comments/
// Author: Miao Zhang
// Date: 2021-03-04
class Solution {
public:
vector<string> removeComments(vector<string>& source) {
vector<string> res;
bool blocked = false;
string out = "";
for (string line: source) {
for (int i = 0; i < line.size(); i++) {
if (!blocked) {
if (i == line.size() - 1) {
out += line[i];
} else {
string t = line.substr(i, 2);
if (t == "/*") {
blocked = true;
i++;
} else if (t == "//") {
break;
} else {
out += line[i];
}
}
} else {
if (i < line.size() - 1) {
string t = line.substr(i, 2);
if (t == "*/") {
blocked = false;
i++;
}
}
}
}
if (!out.empty() && !blocked) {
res.push_back(out);
out = "";
}
}
return res;
}
};