forked from wisdompeak/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path722.Remove-Comments.cpp
More file actions
42 lines (37 loc) · 1.12 KB
/
Copy path722.Remove-Comments.cpp
File metadata and controls
42 lines (37 loc) · 1.12 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
class Solution {
public:
vector<string> removeComments(vector<string>& source)
{
vector<string>result;
bool comment=false;
string s;
for (int i=0; i<source.size(); i++)
{
for (int j=0; j<source[i].size(); j++)
{
if (!comment && j+1<source[i].size() && source[i].substr(j,2)=="//")
break;
else if (!comment && j+1<source[i].size() && source[i].substr(j,2)=="/*")
{
comment=true;
j++;
}
else if (comment && j+1<source[i].size() && source[i].substr(j,2)=="*/")
{
comment=false;
j++;
}
else if (!comment)
{
s.push_back(source[i][j]);
}
}
if (!comment && s.size()>0)
{
result.push_back(s);
s.clear();
}
}
return result;
}
};