-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.h
More file actions
59 lines (57 loc) · 2.19 KB
/
solution.h
File metadata and controls
59 lines (57 loc) · 2.19 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
/*
Code generated by https://github.com/goodstudyqaq/leetcode-local-tester
*/
#if __has_include("../utils/cpp/help.hpp")
#include "../utils/cpp/help.hpp"
#elif __has_include("../../utils/cpp/help.hpp")
#include "../../utils/cpp/help.hpp"
#else
#define debug(...) 42
#endif
class Solution {
public:
vector<string> removeComments(vector<string>& source) {
vector<string> ans;
int n = source.size();
int now = 0;
while (now < n) {
// find // and /*
int idx1 = source[now].find("//");
int idx2 = source[now].find("/*");
if (idx1 == string::npos && idx2 == string::npos) {
// both not found, push back the whole line
ans.push_back(source[now]);
now += 1;
} else {
idx1 = (idx1 == string::npos ? 1e9 :idx1);
idx2 = (idx2 == string::npos ? 1e9 :idx2);
// find the first one
if (idx1 < idx2) {
// // is before /*. So we can just push back the first part
source[now] = source[now].substr(0, idx1);
ans.push_back(source[now]);
now += 1;
} else {
// /* is before //, we need to find */ and merge the rest
string tmp = source[now].substr(0, idx2);
source[now] = source[now].substr(idx2 + 2, source[now].size() - (idx2 + 2)); // Pay attention: it must need to remove the first part.
while (now < n) {
if (source[now].find("*/") != string::npos) {
int idx = source[now].find("*/") + 2;
source[now] = tmp + source[now].substr(idx, source[now].size() - idx);
break;
} else {
now++;
}
}
// After find the */, we just merge and don't need to push back.
}
}
}
vector<string> real_res;
for (auto it : ans) {
if (it.size()) real_res.push_back(it);
}
return real_res;
}
};