Given a C++ program, remove comments from it. The program source
is an array where source[i]
is the i
-th line of the source code. This represents the result of splitting the original source code string by the newline character \n
.
In C++, there are two types of comments, line comments, and block comments.
The string //
denotes a line comment, which represents that it and rest of the characters to the right of it in the same line should be ignored.
The string /*
denotes a block comment, which represents that all characters until the next (non-overlapping) occurrence of */
should be ignored. (Here, occurrences happen in reading order: line by line from left to right.) To be clear, the string /*/
does not yet end the block comment, as the ending would be overlapping the beginning.
The first effective comment takes precedence over others: if the string //
occurs in a block comment, it is ignored. Similarly, if the string /*
occurs in a line or block comment, it is also ignored.
If a certain line of code is empty after removing comments, you must not output that line: each string in the answer list will be non-empty.
There will be no control characters, single quote, or double quote characters. For example, source = "string s = "/* Not a comment. */";"
will not be a test case. (Also, nothing else such as defines or macros will interfere with the comments.)
It is guaranteed that every open block comment will eventually be closed, so /*
outside of a line or block comment always starts a new comment.
Finally, implicit newline characters can be deleted by block comments. Please see the examples below for details.
After removing the comments from the source code, return the source code in the same format.
Example 1:
Input: source = ["/*Test program */", "int main()", "{ ", " // variable declaration ", "int a, b, c;", "/* This is a test", " multiline ", " comment for ", " testing */", "a = b + c;", "}"] The line by line code is visualized as below: /*Test program */ int main() { // variable declaration int a, b, c; /* This is a test multiline comment for testing */ a = b + c; } Output: ["int main()","{ "," ","int a, b, c;","a = b + c;","}"] The line by line code is visualized as below: int main() { int a, b, c; a = b + c; } Explanation: The string/*
denotes a block comment, including line 1 and lines 6-9. The string//
denotes line 4 as comments.
Example 2:
Input: source = ["a/*comment", "line", "more_comment*/b"] Output: ["ab"] Explanation: The original source string is "a/*comment\nline\nmore_comment*/b", where we have bolded the newline characters. After deletion, the implicit newline characters are deleted, leaving the string "ab", which when delimited by newline characters becomes ["ab"].
Note:
- The length of
source
is in the range[1, 100]
. - The length of
source[i]
is in the range[0, 80]
. - Every open block comment is eventually closed.
- There are no single-quote, double-quote, or control characters in the source code.
删除注释。
题目就是题意,我们需要删除input中所有被行内注释(//)和块注释(/**/)包裹住的部分,最后输出其他所有不是注释的部分。注意题目中的一个提示:The first effective comment takes precedence over others。第一个有效注释优先于其他注释。意思是只要开始出现注释的起点,当遇到这个对应的注释终点之前,中间包含的所有部分都是注释,都不能加入结果集。
这道题不涉及算法,算是个实现题吧,需要想清楚几种情况,比较考验细心程度。首先input给的是string array,所以我们可以遍历这个string array,以string的形式遍历整个input。需要考虑行内注释和块注释两种情况。首先对于行内注释比较简单,当遇到“//”的时候,直接就break了,因为从这两个“//”开始,剩下的内容都是注释。对于块注释,稍微复杂一些,这里我们需要用一个boolean flag来记录我们是否遇到了一个块注释的起点。当我们遇到一个“/*”的时候,我们标记一下,表示我们开始处理块注释了,此时只需要i++,直到我们遇到块注释的结尾 “*/”。最后注意当每个string被加入结果集之后,需要把string builder清空,以处理下一个string。
时间O(mn) - string的平均长度 * string array的长度
空间O(1)
Java实现
1 class Solution { 2 public List<String> removeComments(String[] source) { 3 List<String> res = new ArrayList<>(); 4 StringBuilder sb = new StringBuilder(); 5 // whether in the multi line comment mode or not 6 boolean mode = false; 7 for (String s : source) { 8 for (int i = 0; i < s.length(); i++) { 9 if (mode) { 10 if (s.charAt(i) == '*' && i < s.length() - 1 && s.charAt(i + 1) == '/') { 11 mode = false; 12 // skip '/' on next iteration of i 13 i++; 14 } 15 } else { 16 // if we see a single line comment 17 if (s.charAt(i) == '/' && i < s.length() - 1 && s.charAt(i + 1) == '/') { 18 break; 19 } else if (s.charAt(i) == '/' && i < s.length() - 1 && s.charAt(i + 1) == '*') { 20 mode = true; 21 // skip '*' on next iteration of i 22 i++; 23 } else { 24 // not a comment 25 sb.append(s.charAt(i)); 26 } 27 } 28 } 29 if (!mode && sb.length() > 0) { 30 res.add(sb.toString()); 31 sb = new StringBuilder(); 32 } 33 } 34 return res; 35 } 36 }