-
Notifications
You must be signed in to change notification settings - Fork 3
/
722.cpp
35 lines (35 loc) · 1.13 KB
/
722.cpp
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
class Solution {
public:
vector<string> removeComments(vector<string>& source) {
bool commentFlag = false;
vector<string> res;
string temp;
for (auto& s : source) {
int n = s.size();
for (int i = 0; i < n; ++i) {
if (!commentFlag) {
if (s[i] == '/') {
if (i + 1 < n && s[i + 1] == '/') {
break;
}
else if (i + 1 < n && s[i + 1] == '*') {
commentFlag = true;
i++;
// case: /*/
if (i + 1 < n && s[i + 1] == '/') i++;
}
}
if (!commentFlag) temp.push_back(s[i]);
}
else {
if (s[i] == '/' && i > 0 && s[i - 1] == '*') commentFlag = false;
}
}
if (!commentFlag && temp.size() != 0) {
res.push_back(temp);
temp.clear();
}
}
return res;
}
};