#Problem

#⭐️⭐️

Remove Comments

#Solution

Edge cases贼多. 特别要多考虑的一点就是*/后面的东西(如果还有的话), 要接到/*之前的string上.

一开始想着每一行单独用一个StringBuilder, 然后记住open block的行数, 当遇到close block的时候:

1
res.set(lastOpenBlock, res.get(lastOpenBlock) + builder.toString());

这样代码写的太麻烦了. 其实不需要每一行单独用一个StringBuilder, 可以跨行. 这样在每行结束的时候, 我们只要判断一下是不是在open block里面, 是的话就不需要把builder加进result set里. 很好的避免了不少复杂的代码.

#Code

#Java
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// 太复杂了, 看后面一个solution
class Solution {
public List<String> removeComments(String[] source) {
final List<String> res = new ArrayList<>();
int open = 0;
int lastOpenBlock = -1;
for (final String s : source) {
if (s.isEmpty()) continue;
final StringBuilder builder = new StringBuilder();
final char[] chs = s.toCharArray();
for (int i = 0; i < chs.length; i++) {
final char c = chs[i];
if (c == '/') {
if (open == 0 && i != chs.length - 1 && chs[i + 1] == '/') {
// The rest of this line are all line comments.
break;
} else if (open == 0 && i != chs.length - 1 && chs[i + 1] == '*') {
// Find a open block comment.
open++;
if (i != 0) {
lastOpenBlock = res.size();
}
i++;
} else if (open == 0) {
// This is maybe due to the arithmetic operation like x = a / b;
builder.append(c);
}
} else if (c == '*') {
if (open != 0 && i != chs.length - 1 && chs[i + 1] == '/') {
// Find a close block comment.
open--;
if (lastOpenBlock == res.size()) {
// This means the /* and */ appear on the same line.
lastOpenBlock = -1;
}
// Try to find if anything else after the "*/"
// If there is any, we should append the rest of them to
// the string at lastOpenBlock
int j = i + 2;
while (j < chs.length && chs[j] == ' ') {
builder.append(chs[j]);
j++;
}
if (j == chs.length) {
lastOpenBlock = -1;
break;
} else {
i = j - 1;
}
} else if (open == 0) {
// This is maybe due to the arithmetic operation like x = a * b;
builder.append(c);
}
} else if (open == 0) {
builder.append(c);
}
}
if (builder.length() != 0) {
if (lastOpenBlock == -1 || lastOpenBlock == res.size()) {
res.add(builder.toString());
} else {
res.set(lastOpenBlock, res.get(lastOpenBlock) + builder.toString());
lastOpenBlock = -1;
}
}
}
return res;
}
}
class Solution {
public List<String> removeComments(String[] source) {
final List<String> res = new ArrayList<>();
boolean open = false;
final StrngBuilder builder = new StringBuilder();
for (final String s : source) {
if (s.isEmpty()) continue;
final char[] chs = s.toCharArray();
for (int i = 0; i < chs.length; i++) {
if (open) {
if (c == '*' && i != chs.length - 1 && chs[i + 1] == '/') {
open = false;
i++;
}
} else {
if (c == '/' && i != chs.length - 1 && chs[i + 1] == '/') {
break;
} else if (c == '/' && i != chs.length - 1 && chs[i + 1] == '*') {
open = true;
i++;
} else {
builder.append(c);
}
}
}
if (!open && builder.length() > 0) {
res.add(builder.toString());
builder.setLength(0);
}
}
return res;
}
}

Comments