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
| class Solution { public List<String> removeComments(String[] source) { List<String> ans = new ArrayList<>(); StringBuilder builder = new StringBuilder(); boolean blockComment = false; for (String s : source) { int n = s.length(); for (int i = 0; i < n; ++i) { if (blockComment) { if (i + 1 < n && s.charAt(i) == '*' && s.charAt(i + 1) == '/') { blockComment = false; ++i; } } else { if (i + 1 < n && s.charAt(i) == '/' && s.charAt(i + 1) == '*') { blockComment = true; ++i; } else if (i + 1 < n && s.charAt(i) == '/' && s.charAt(i + 1) == '/') { break; } else { builder.append(s.charAt(i)); } } } if (!blockComment && !builder.isEmpty()) { ans.add(builder.toString()); builder.setLength(0); } } return ans; } }
|