英文原文
Given a C++ program, remove comments from it. The program source is an array of strings source
where source[i]
is the ith
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 the 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.
- For example, 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;", "}"] Output: ["int main()","{ "," ","int a, b, c;","a = b + c;","}"] Explanation: 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; } The string /* denotes a block comment, including line 1 and lines 6-9. The string // denotes line 4 as comments. The line by line output code is visualized as below: int main() { int a, b, c; a = b + c; }
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"].
Constraints:
1 <= source.length <= 100
0 <= source[i].length <= 80
source[i]
consists of printable ASCII characters.- Every open block comment is eventually closed.
- There are no single-quote or double-quote in the input.
中文题目
给一个 C++ 程序,删除程序中的注释。这个程序source
是一个数组,其中source[i]
表示第i
行源码。 这表示每行源码由\n
分隔。
在 C++ 中有两种注释风格,行内注释和块注释。
字符串//
表示行注释,表示//
和其右侧的其余字符应该被忽略。
字符串/*
表示一个块注释,它表示直到*/
的下一个(非重叠)出现的所有字符都应该被忽略。(阅读顺序为从左到右)非重叠是指,字符串/*/
并没有结束块注释,因为注释的结尾与开头相重叠。
第一个有效注释优先于其他注释:如果字符串//
出现在块注释中会被忽略。 同样,如果字符串/*
出现在行或块注释中也会被忽略。
如果一行在删除注释之后变为空字符串,那么不要输出该行。即,答案列表中的每个字符串都是非空的。
样例中没有控制字符,单引号或双引号字符。比如,source = "string s = "/* Not a comment. */";"
不会出现在测试样例里。(此外,没有其他内容(如定义或宏)会干扰注释。)
我们保证每一个块注释最终都会被闭合, 所以在行或块注释之外的/*
总是开始新的注释。
最后,隐式换行符可以通过块注释删除。 有关详细信息,请参阅下面的示例。
从源代码中删除注释后,需要以相同的格式返回源代码。
示例 1:
输入: source = ["/*Test program */", "int main()", "{ ", " // variable declaration ", "int a, b, c;", "/* This is a test", " multiline ", " comment for ", " testing */", "a = b + c;", "}"] 示例代码可以编排成这样: /*Test program */ int main() { // variable declaration int a, b, c; /* This is a test multiline comment for testing */ a = b + c; } 输出: ["int main()","{ "," ","int a, b, c;","a = b + c;","}"] 编排后: int main() { int a, b, c; a = b + c; } 解释: 第 1 行和第 6-9 行的字符串 /* 表示块注释。第 4 行的字符串 // 表示行注释。
示例 2:
输入: source = ["a/*comment", "line", "more_comment*/b"] 输出: ["ab"] 解释: 原始的 source 字符串是 "a/*comment\nline\nmore_comment*/b", 其中我们用粗体显示了换行符。删除注释后,隐含的换行符被删除,留下字符串 "ab" 用换行符分隔成数组时就是 ["ab"].
注意:
source
的长度范围为[1, 100]
.source[i]
的长度范围为[0, 80]
.- 每个块注释都会被闭合。
- 给定的源码中不会有单引号、双引号或其他控制字符。
通过代码
官方题解
算法:
- 我们需要逐行分析源代码。有两种情况,要么在一个注释内或者不在。
- 如果我们遇到注释块符号,而我们不在注释中,那么我们将跳过接下来的两个字符,并将状态更改为在注释中。
- 如果我们遇到注释块符号并且我们在注释中,那么我们将跳过接下来的两个字符并将状态更改为不在注释中。
- 如果我们遇到一个行注释且我们不在注释中,那么我们将忽略该行的其余部分。
- 如果我们不在注释中(并且它不是注释的开头),我们将记录所遇到的字符。
- 在每行的末尾,如果我们不在注释中,我们将记录该行。
class Solution(object):
def removeComments(self, source):
in_block = False
ans = []
for line in source:
i = 0
if not in_block:
newline = []
while i < len(line):
if line[i:i+2] == '/*' and not in_block:
in_block = True
i += 1
elif line[i:i+2] == '*/' and in_block:
in_block = False
i += 1
elif not in_block and line[i:i+2] == '//':
break
elif not in_block:
newline.append(line[i])
i += 1
if newline and not in_block:
ans.append("".join(newline))
return ans
class Solution {
public List<String> removeComments(String[] source) {
boolean inBlock = false;
StringBuilder newline = new StringBuilder();
List<String> ans = new ArrayList();
for (String line: source) {
int i = 0;
char[] chars = line.toCharArray();
if (!inBlock) newline = new StringBuilder();
while (i < line.length()) {
if (!inBlock && i+1 < line.length() && chars[i] == '/' && chars[i+1] == '*') {
inBlock = true;
i++;
} else if (inBlock && i+1 < line.length() && chars[i] == '*' && chars[i+1] == '/') {
inBlock = false;
i++;
} else if (!inBlock && i+1 < line.length() && chars[i] == '/' && chars[i+1] == '/') {
break;
} else if (!inBlock) {
newline.append(chars[i]);
}
i++;
}
if (!inBlock && newline.length() > 0) {
ans.add(new String(newline));
}
}
return ans;
}
}
复杂度分析
- 时间复杂度:$O(S)$。$S$ 指的是源代码的长度。
- 空间复杂度:$O(S)$,将源代码记录到
ans
中所使用的空间。
统计信息
通过次数 | 提交次数 | AC比率 |
---|---|---|
5251 | 16239 | 32.3% |
提交历史
提交时间 | 提交结果 | 执行时间 | 内存消耗 | 语言 |
---|
相似题目
题目 | 难度 |
---|---|
迷你语法分析器 | 中等 |
三元表达式解析器 | 中等 |