-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremove-comments.cpp
More file actions
41 lines (36 loc) · 1.22 KB
/
remove-comments.cpp
File metadata and controls
41 lines (36 loc) · 1.22 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
//https://leetcode.com/problems/remove-comments/submissions/
class Solution {
public:
vector<string> removeComments(vector<string>& source) {
vector<string> res;
bool in_block = false;
for (auto str:source)
{
int i = 0;
static string temp ;
if (!in_block)
temp = "";
while (i < str.length())
{
if ( (str[i] == '/') && (str[i+1] == '/') && !in_block)
break;
else if ( (str[i] == '/') && (str[i+1] == '*') && !in_block)
{
in_block = true;
i+=1;
}
else if ( (str[i] == '*') && (str[i+1] == '/') && in_block)
{
in_block = false;
i+=1;
}
else if(!in_block)
temp += str[i];
i +=1;
}
if (temp.size() > 0 && !in_block)
res.push_back(temp);
}
return res;
}
};