[LeetCode] 722. Remove Comments

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.

删除注释。

给一个 C++ 程序,删除程序中的注释。这个程序source是一个数组,其中source[i]表示第 i 行源码。 这表示每行源码由 '\n' 分隔。

在 C++ 中有两种注释风格,行内注释和块注释。

字符串// 表示行注释,表示//和其右侧的其余字符应该被忽略。
字符串/* 表示一个块注释,它表示直到下一个(非重叠)出现的*/之间的所有字符都应该被忽略。(阅读顺序为从左到右)非重叠是指,字符串/*/并没有结束块注释,因为注释的结尾与开头相重叠。
第一个有效注释优先于其他注释。

如果字符串//出现在块注释中会被忽略。
同样,如果字符串/*出现在行或块注释中也会被忽略。
如果一行在删除注释之后变为空字符串,那么不要输出该行。即,答案列表中的每个字符串都是非空的。

样例中没有控制字符,单引号或双引号字符。

比如,source = "string s = "/* Not a comment. */";" 不会出现在测试样例里。
此外,没有其他内容(如定义或宏)会干扰注释。

我们保证每一个块注释最终都会被闭合, 所以在行或块注释之外的/*总是开始新的注释。

最后,隐式换行符可以通过块注释删除。 有关详细信息,请参阅下面的示例。

从源代码中删除注释后,需要以相同的格式返回源代码。

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/remove-comments
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

我们需要删除 input 中所有被行内注释(//)和块注释(/**/)包裹住的部分,最后输出其他所有不是注释的部分。注意题目中的一个提示:The first effective comment takes precedence over others。第一个有效注释优先于其他注释。意思是只要开始出现注释的起点,当遇到这个对应的注释终点之前,中间包含的所有部分都是注释,都不能加入结果集。块注释处理的难点在于块注释里可能包含行注释。

具体做法是遍历 source 的每一行,对于当前行,我们判断到底属于以下哪种情况

  • 如果 flag == true 说明我们还在块注释中,我们要找块注释的结尾("*/"),只要没找到,当前行就要跳过
  • 如果 flag == false 说明我们不在块注释中,
  • 如果遇到 // ,说明遇到行注释,跳过当前行
  • 如果遇到块注释的开头("/*"),就标记 flag = true
  • 其他情况则将字符加入 stringbuilder

时间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 }

 

LeetCode 题目总结

posted @ 2020-11-04 05:49  CNoodle  阅读(187)  评论(0编辑  收藏  举报