-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathRemoveComments.java
47 lines (46 loc) · 1.67 KB
/
RemoveComments.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
/*https://leetcode.com/problems/remove-comments/*/
class Solution {
public List<String> removeComments(String[] source) {
List<String> result = new ArrayList<String>();
StringBuilder build = new StringBuilder("");
boolean commentBlock = false, commentLine = false;
for (String line : source)
{
commentLine = false;
for (int i = 0; i < line.length(); ++i)
{
if (i < line.length()-1)
{
if (line.charAt(i) == '/')
{
if (line.charAt(i+1) == '*' && !commentBlock && !commentLine)
{
commentBlock = true;
++i;
continue;
}
else if (line.charAt(i+1) == '/' && !commentBlock)
{
commentLine = true;
++i;
continue;
}
}
if (i < line.length()-1 && line.charAt(i) == '*' && line.charAt(i+1) == '/' && commentBlock)
{
commentBlock = false;
++i;
continue;
}
}
if (!commentLine && !commentBlock && i < line.length()) build.append(line.charAt(i));
}
if (build.length() > 0 && !commentBlock)
{
result.add(build.toString());
build = new StringBuilder("");
}
}
return result;
}
}