forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRemove Comments.cpp
More file actions
40 lines (39 loc) · 1.31 KB
/
Remove Comments.cpp
File metadata and controls
40 lines (39 loc) · 1.31 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
class Solution {
public:
vector<string> removeComments(vector<string>& source) {
bool commentStart = false;
vector<string> res;
bool multiComment = false; // are we having a multi-line comment?
for (string &eachS : source) {
if (!multiComment) {
res.emplace_back();
}
for (int i = 0; i < eachS.size(); i++) {
if (!multiComment && eachS[i] == '/') {
i++;
if (eachS[i] == '/') {
break;
} else if (eachS[i] == '*') {
multiComment = true;
} else {
res.back() += '/';
res.back() += eachS[i];
}
} else if (multiComment && eachS[i] == '*') {
if (i + 1 < eachS.size() && eachS[i + 1] == '/') {
i++;
multiComment = false;
}
} else {
if (!multiComment) {
res.back() += eachS[i];
}
}
}
if (!multiComment && res.back().empty()) {
res.pop_back();
}
}
return res;
}
};