-
Notifications
You must be signed in to change notification settings - Fork 0
/
remove_comments.cc
35 lines (31 loc) · 963 Bytes
/
remove_comments.cc
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) {
vector<string> s;
string build;
bool block_comment = false;
for (auto line : source) {
for (int i = 0; i < line.size(); i++) {
string next_two = line.substr(i, 2);
if (!block_comment) {
if (next_two == "//")
break;
else if (next_two == "/*") {
block_comment = true;
i++;
}
else
build.push_back(line[i]);
} else if (next_two == "*/") {
block_comment = false;
i++;
}
}
if (!block_comment && !build.empty()) {
s.push_back(build);
build.clear();
}
}
return s;
}
};