leetcode链接:722. 删除注释
题目分析



方案一
模拟方法。
好混乱,没写出来
Java split() 方法
https://www.runoob.com/java/java-string-split.html
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
| class Solution { public List<String> removeComments(String[] source) { List<String> result = new ArrayList<>();
boolean flag = true; StringBuilder strFlag = new StringBuilder(); for (String str : source) { if (flag && str.contains("//")) { String[] split = str.split("//"); if (split[0].length() > 0) { result.add(split[0]); } } else if (flag && str.contains("/*")) { String[] split = str.split("/\\*"); strFlag.append(split[0]); flag = false; if (split[split.length - 1].contains("*/")) { flag = true; String[] split1 = split[split.length - 1].split("\\*/"); if (split1.length > 1) { strFlag.append(split1[split1.length - 1]); } if (strFlag.length() > 0) { result.add(strFlag.toString()); strFlag = new StringBuilder(); } } } else if (!flag && str.contains("*/")) { flag = true; String[] split = str.split("\\*/"); if (split.length > 1) { strFlag.append(split[split.length - 1]); } if (strFlag.length() > 0) { result.add(strFlag.toString()); } } else if (flag){ result.add(str); } }
return result; } }
|
结果
解答错误
28 / 54 个通过测试用例
方案二
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> result = new ArrayList<>();
boolean flag = true; StringBuilder strFlag = new StringBuilder(); for (String str : source) { for (int i = 0; i < str.length(); i++) { if (flag) { if (i + 1 < str.length() && str.charAt(i) == '/' && str.charAt(i + 1) == '/') { break; } else if (i + 1 < str.length() && str.charAt(i) == '/' && str.charAt(i + 1) == '*') { flag = false; i++; } else { strFlag.append(str.charAt(i)); } } else { if (i + 1 < str.length() && str.charAt(i) == '*' && str.charAt(i + 1) == '/') { flag = true; i++; } } }
if (flag && strFlag.length() > 0) { result.add(strFlag.toString()); strFlag = new StringBuilder(); } }
return result; } }
|
结果
解答成功:
执行耗时:0 ms,击败了100.00% 的Java用户
内存消耗:40.1 MB,击败了55.14% 的Java用户
官方题解
https://leetcode.cn/problems/remove-comments/solution/shan-chu-zhu-shi-by-leetcode-solution-lb9x/

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
| class Solution { public List<String> removeComments(String[] source) { List<String> res = new ArrayList<String>(); StringBuilder newLine = new StringBuilder(); boolean inBlock = false; for (String line : source) { for (int i = 0; i < line.length(); i++) { if (inBlock) { if (i + 1 < line.length() && line.charAt(i) == '*' && line.charAt(i + 1) == '/') { inBlock = false; i++; } } else { if (i + 1 < line.length() && line.charAt(i) == '/' && line.charAt(i + 1) == '*') { inBlock = true; i++; } else if (i + 1 < line.length() && line.charAt(i) == '/' && line.charAt(i + 1) == '/') { break; } else { newLine.append(line.charAt(i)); } } } if (!inBlock && newLine.length() > 0) { res.add(newLine.toString()); newLine.setLength(0); } } return res; } }
作者:LeetCode-Solution 链接:https: 来源:力扣(LeetCode) 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
|
