-
-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy path722.cpp
More file actions
67 lines (65 loc) · 2.3 KB
/
722.cpp
File metadata and controls
67 lines (65 loc) · 2.3 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
__________________________________________________________________________________________________
sample 4 ms submission
class Solution {
public:
vector<string> removeComments(vector<string>& source) {
vector<string> res;
bool comment = false;
string s = "";
for(auto &line : source) {
int len = line.length();
for(int i = 0; i < len; i++) {
if(!comment and i + 1 < len and line[i] == '/' and line[i + 1] == '/') {
break;
} else if(!comment and i + 1 < len and line[i] == '/' and line[i + 1] == '*') {
comment = true;
i++;
} else if(comment and i + 1 < len and line[i] == '*' and line[i + 1] == '/') {
comment = false;
i++;
} else if(!comment) {
s.push_back(line[i]);
}
}
if(s.size() and !comment) {
res.push_back(s);
s.clear();
}
}
return res;
}
};
__________________________________________________________________________________________________
sample 8920 kb submission
class Solution {
public:
vector<string> removeComments(vector<string>& source) {
vector<string> res;
bool blocked = false;
string out = "";
for (string line : source) {
for (int i = 0; i < line.size(); ++i) {
if (!blocked) {
if (i == line.size() - 1) out += line[i];
else {
string t = line.substr(i, 2);
if (t == "/*") blocked = true, ++i;
else if (t == "//") break;
else out += line[i];
}
} else {
if (i < line.size() - 1) {
string t = line.substr(i, 2);
if (t == "*/") blocked = false, ++i;
}
}
}
if (!out.empty() && !blocked) {
res.push_back(out);
out = "";
}
}
return res;
}
};
__________________________________________________________________________________________________