-
Notifications
You must be signed in to change notification settings - Fork 383
Expand file tree
/
Copy pathRemove Comments.java
More file actions
34 lines (34 loc) · 1002 Bytes
/
Remove Comments.java
File metadata and controls
34 lines (34 loc) · 1002 Bytes
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
class Solution {
public List<String> removeComments(String[] source) {
StringBuilder sb = new StringBuilder();
List<String> list = new ArrayList<>();
boolean blockComment = false;
for (String word : source) {
for (int i = 0; i < word.length(); i++) {
if (!blockComment) {
if ((i + 1) < word.length() && word.charAt(i) == '/' && word.charAt(i + 1) == '/') {
break;
}
else if ((i + 1) < word.length() && word.charAt(i) == '/' && word.charAt(i + 1) == '*') {
blockComment = true;
i++;
}
else {
sb.append(word.charAt(i));
}
}
else {
if ((i + 1) < word.length() && word.charAt(i) == '*' && word.charAt(i + 1) == '/') {
blockComment = false;
i++;
}
}
}
if (!blockComment && sb.length() > 0) {
list.add(sb.toString());
sb.setLength(0);
}
}
return list;
}
}