05 August 2008
给一个 C++ 程序,删除程序中的注释。这个程序source是一个数组,其中source[i]表示第i行源码。 这表示每行源码由\n分隔。
在 C++ 中有两种注释风格,行内注释和块注释。
字符串// 表示行注释,表示//和其右侧的其余字符应该被忽略。
字符串/ 表示一个块注释,它表示直到/的下一个(非重叠)出现的所有字符都应该被忽略。(阅读顺序为从左到右)非重叠是指,字符串/*/并没有结束块注释,因为注释的结尾与开头相重叠。
第一个有效注释优先于其他注释:如果字符串//出现在块注释中会被忽略。 同样,如果字符串/*出现在行或块注释中也会被忽略。
如果一行在删除注释之后变为空字符串,那么不要输出该行。即,答案列表中的每个字符串都是非空的。
样例中没有控制字符,单引号或双引号字符。比如,source = “string s = “/ Not a comment. /”;” 不会出现在测试样例里。(此外,没有其他内容(如定义或宏)会干扰注释。)
我们保证每一个块注释最终都会被闭合, 所以在行或块注释之外的/*总是开始新的注释。
最后,隐式换行符可以通过块注释删除。 有关详细信息,请参阅下面的示例。
从源代码中删除注释后,需要以相同的格式返回源代码。
Given a C++ program, remove comments from it. The program
is an array where 1
source
is the 1
source[i]
-th line of the source code. This represents the result of splitting the original source code string by the newline character 1
i
.1
\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.1
//
The string
denotes a block comment, which represents that all characters until the next (non-overlapping) occurrence of 1
/*
should be ignored. (Here, occurrences happen in reading order: line by line from left to right.) To be clear, the string 1
*/
does not yet end the block comment, as the ending would be overlapping the beginning.1
/*/
The first effective comment takes precedence over others: if the string
occurs in a block comment, it is ignored. Similarly, if the string 1
//
occurs in a line or block comment, it is also ignored.1
/*
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,
will not be a test case. (Also, nothing else such as defines or macros will interfere with the comments.)1
source = "string s = "/* Not a comment. */";"
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.1
/*
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:
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
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:
1
2
3
4
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
is in the range 1
source
.1
[1, 100]
The length of
is in the range 1
source[i]
.1
[0, 80]
Every open block comment is eventually closed.
There are no single-quote, double-quote, or control characters in the source code.
需要逐行分析源代码。此题是考验字符串处理的能力 有五种状态:
算法如下:
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
48
49
class Solution {
public List<String> removeComments(String[] source) {
boolean isLineComment = false;
boolean isBlockComment = false;
StringBuilder sbLine = null;
List<String> resultList = new ArrayList<>();
for(String strLine : source){
int strLen = strLine.length();
int cursor = 0;
if(!isBlockComment){
sbLine = new StringBuilder();
}
while(cursor < strLen){
if(isLineComment){
isLineComment = false;
break;//进入下一行
}else if(isBlockComment){
if(strLine.charAt(cursor) == '*'
&& cursor < strLen - 1 && strLine.charAt(cursor + 1) == '/'
/*&& cursor > 0 && strLine.charAt(cursor - 1) != '/'*/){
isBlockComment = false;
cursor++;
}
}else{//两个注释都没有
//是否可能有注释
if(strLine.charAt(cursor) == '/'){
if(cursor < strLen - 1 && strLine.charAt(cursor + 1) == '/'){
isLineComment = true;
cursor++;
}else if(cursor < strLen - 1 && strLine.charAt(cursor + 1) == '*'){
isBlockComment = true;
cursor++;
}else{
sbLine.append(strLine.charAt(cursor));
}
}else{
sbLine.append(strLine.charAt(cursor));
}
}
cursor++;
}
if(!isBlockComment && sbLine.length() > 0){
resultList.add(sbLine.toString());
}
}
return resultList;
}
}