-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathremoveComments.cpp
55 lines (52 loc) · 2.06 KB
/
removeComments.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
class Solution {
public:
vector<string> removeComments(vector<string>& source) {
if(source.size() == 0) return {};
int i, j;
bool is_comment = false;
vector<string> result;
string s;
for(i = 0; i<source.size();i++){
for(j = 0; j<source[i].size();j++){
/*if(j < source[i].size()-1){
if(!is_comment){
if(source[i][j] == '/' && source[i][j+1] == '/'){
break;
}
else if(source[i][j] == '/' && source[i][j+1] == '*'){
is_comment = true; j++;
}
}
else if(is_comment){
if(source[i][j] == '*' && source[i][j+1] == '/'){
is_comment = false; j++;
}
}
else if(!is_comment) s.push_back(source[i][j]);
}*/
if(!is_comment && j < source[i].size() - 1 && source[i][j] == '/' && source[i][j+1] == '/'){ // just break and goto next line
break;
}
else if(!is_comment && j < source[i].size() && source[i][j] == '/' && source[i][j+1] == '*'){
is_comment = true; //now we dont care about other characters
j++;
}
else if(is_comment && j < source[i].size() && source[i][j] == '*' && source[i][j+1] == '/'){
is_comment = false;
j++;
}
else if(!is_comment){ //normal character)
s.push_back(source[i][j]);
}
}
if(!is_comment) result.push_back(s), s.clear();
}
vector<string> f_res;
for(string r : result){
if(r != "")
f_res.push_back(r);
}
return f_res;
//return result;
}
};