Skip to content

Commit cd99779

Browse files
authored
Create 722.Remove-Comments.cpp
1 parent 1f461a4 commit cd99779

1 file changed

Lines changed: 42 additions & 0 deletions

File tree

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
class Solution {
2+
public:
3+
vector<string> removeComments(vector<string>& source)
4+
{
5+
vector<string>result;
6+
bool comment=false;
7+
string s;
8+
9+
for (int i=0; i<source.size(); i++)
10+
{
11+
for (int j=0; j<source[i].size(); j++)
12+
{
13+
if (!comment && j+1<source[i].size() && source[i].substr(j,2)=="//")
14+
break;
15+
else if (!comment && j+1<source[i].size() && source[i].substr(j,2)=="/*")
16+
{
17+
comment=true;
18+
j++;
19+
}
20+
else if (comment && j+1<source[i].size() && source[i].substr(j,2)=="*/")
21+
{
22+
comment=false;
23+
j++;
24+
}
25+
else if (!comment)
26+
{
27+
s.push_back(source[i][j]);
28+
}
29+
30+
}
31+
32+
if (!comment && s.size()>0)
33+
{
34+
result.push_back(s);
35+
s.clear();
36+
}
37+
38+
}
39+
40+
return result;
41+
}
42+
};

0 commit comments

Comments
 (0)