原题链接在这里:https://leetcode.com/problems/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.

题解:

First we need to track if it is within /* ... */ comment section.

If it is in the comment section, the only thing we care is to find closing comment */. Other things, we don't care.

If not, when encountering //, break the current line.

when encountering /*, make isComment as true, skip *.

Otherwise, append current char to StringBuilder.

After each line, check if it is not comennt section and sb length > 0. Add it to res.

Time Complexity: O(n*m). m = max(line length).

Space: O(m).

AC Java:

 class Solution {
public List<String> removeComments(String[] source) {
List<String> res = new ArrayList<>();
StringBuilder sb = new StringBuilder();
boolean isComment = false;
for(String s : source){
for(int i = 0; i<s.length(); i++){
if(isComment){
if(s.charAt(i) == '*' && i+1<s.length() && s.charAt(i+1) == '/'){
isComment = false;
i++; // Skip / in "*/""
}
}else{
if(s.charAt(i) == '/' && i+1<s.length() && s.charAt(i+1) =='/'){
break;
}else if(s.charAt(i) == '/' && i+1<s.length() && s.charAt(i+1) == '*'){
isComment = true;
i++; // Skip * in "/*"
}else{
sb.append(s.charAt(i));
}
}
} if(!isComment && sb.length() > 0){
res.add(sb.toString());
sb = new StringBuilder();
}
} return res;
}
}

LeetCode 722. Remove Comments的更多相关文章

  1. 【LeetCode】722. Remove Comments 解题报告(Python)

    [LeetCode]722. Remove Comments 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/remove-c ...

  2. 【leetcode】722. Remove Comments

    题目如下: Given a C++ program, remove comments from it. The program source is an array where source[i] i ...

  3. LC 722. Remove Comments

    Given a C++ program, remove comments from it. The program source is an array where source[i] is the  ...

  4. 722. Remove Comments

    class Solution { public: vector<string> removeComments(vector<string>& source) { vec ...

  5. [LeetCode] Remove Comments 移除注释

    Given a C++ program, remove comments from it. The program source is an array where source[i] is the ...

  6. [Swift]LeetCode722. 删除注释 | Remove Comments

    Given a C++ program, remove comments from it. The program source is an array where source[i] is the  ...

  7. LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>

    LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...

  8. LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] <c++>

    LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] <c++> 给出排序好的 ...

  9. [LeetCode] 80. Remove Duplicates from Sorted Array II ☆☆☆(从有序数组中删除重复项之二)

    https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/discuss/27976/3-6-easy-lines-C% ...

随机推荐

  1. 【题解】整数划分 [51nod1201] 整数划分 V2 [51nod1259]

    [题解]整数划分 [51nod1201] 整数划分 V2 [51nod1259] 传送门:整数划分 \([51nod1201]\) 整数划分 \(V2\) \([51nod1259]\)** [题目描 ...

  2. 菜刀连接一句话木马出现:`Cannot call assert() with string argument dynamically`错误

    前言 逆天还是上学那会玩渗透的,后来工作后就再也没碰了,所以用的工具还是以前经典款,这不,发现出问题了 问题 如果是PHP5则没有问题,如果是PHP7,会出现:Cannot call assert() ...

  3. C# - Array.Sort()方法

    Array类简介 Array类是C#中所有数组的基类.我们常用的[]声明数组即为Array类的语法,我们可通过Array类提供的各种方法对C#中数组进行操作.最典型的就是数组排序 Array.Sort ...

  4. typescript nodejs 依赖注入实现

    依赖注入通常也是我们所说的ioc模式,今天分享的是用typescript语言实现的ioc模式,这边用到的主要组件是 reflect-metadata 这个组件可以获取或者设置元数据信息,它的作用是拿到 ...

  5. The instance of entity type 'Menu' cannot be tracked because another instance with the same key value for {'Id'} is already being tracked.

    这里记录一个在使用.net core中ef core执行数据库操作时遇到的问题: 我在代码中使用DbContext下的Update方法准备将更改后的数据像这样步到数据库: _context.Menus ...

  6. Lumen 使用事件需要注意的事项

    Lumen 版本 5.2 参考手册 laravel event 需要注意的事项 如果是第一次在lumen下使用事件,需要修改bootstrap\app.php文件 添加对EventServicePro ...

  7. maven下载,上传设置

    <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3. ...

  8. 排序算法的c++实现——快速排序

    快速排序是分治思想的又一典型代表,是应用最广的排序算法.分治思想就是把原问题的解分解为两个或多个子问题解,求解出子问题的解之后再构造出原问题的解. 在快速排序算法中,它的思想是把一个待排序的数组分成前 ...

  9. 阿里云ECS-使用putty产品psftp工具上传下载

    本人windows10,安装了winscp3,原本可以简单易用,但天空不作美,死活不让我连接,无奈,只能换命令行方式, 好在,putty提供了一个小工具,psftp,不过,需要去官网下载完整版才有哦, ...

  10. MTU是什么?

    MTU是Maximum Transmission Unit的缩写. 意思是网络上传送的最大数据包. MTU的单位是字节. 大部分网络设备的MTU都是1500.如果本机的MTU比网关的MTU大,大的数据 ...