forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.ts
30 lines (30 loc) · 901 Bytes
/
Solution.ts
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
function removeComments(source: string[]): string[] {
const ans: string[] = [];
const t: string[] = [];
let blockComment = false;
for (const s of source) {
const m = s.length;
for (let i = 0; i < m; ++i) {
if (blockComment) {
if (i + 1 < m && s.slice(i, i + 2) === '*/') {
blockComment = false;
++i;
}
} else {
if (i + 1 < m && s.slice(i, i + 2) === '/*') {
blockComment = true;
++i;
} else if (i + 1 < m && s.slice(i, i + 2) === '//') {
break;
} else {
t.push(s[i]);
}
}
}
if (!blockComment && t.length) {
ans.push(t.join(''));
t.length = 0;
}
}
return ans;
}