原题链接在这里: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. 【chromium】 渲染显示相关概念

    DRM(Direct Rendering Manager) DRM 由两个部分组成:一是 Kernel 的子系统,这个子系统对硬件 GPU 操作进行了一层框架封装.二是提供了一个 libdrm 库,里 ...

  2. Win10 UEFI 系统安装教程

    1:首先我们需要先拿一个U盘,制作一个带UEFI PE.(网上的大白菜以及老毛桃都可以,我用的是从网上找的WIN10 PE.WIN10 PE的好处是集成了NVME驱动,可以认得到SM951 NVME版 ...

  3. SQL server中常用sql语句

    --循环执行插入10000条数据 declare @ID intbeginset @ID=1 while @ID<=10000begininsert into table_name values ...

  4. RabbitMQ学习之Publish/Subscribe(3)

    上一个教程中,我们创建了一个work queue. 其中的每个task都会被精确的传送到一个worker. 这节,我们将会讲把一个message传送到多个consumers. 这种模式叫做publis ...

  5. C# vb .net图像合成-合成自定义路径

    在.net中,如何简单快捷地实现图像合成呢,比如合成文字,合成艺术字,多张图片叠加合成等等?答案是调用SharpImage!专业图像特效滤镜和合成类库.下面开始演示关键代码,您也可以在文末下载全部源码 ...

  6. SpringMVC中@RequestParam注解作用

    1.不使用@RequestParam  请求参数名必须和形参名称一样 2.使用@RequestParam    请求参数名必须和@RequestParam value属性值一样    请求参数名不必和 ...

  7. Cheat Engine 作弊表框架代码

    打开游戏 打开自动汇编 扫描的所有过程,这里就省略了 引用作弊表框架代码 查找使阳光减少的地址 拷贝这个地址 添加到自动汇编脚本里,并添加汇编指令 分配到当前作弊表 生成自动汇编脚本 进行激活测试 可 ...

  8. Elasticsearch使用DateHistogram聚合

    date_histogram是按照时间来构建集合(桶)Buckts的,当我们需要按照时间进行做一些数据统计的时候,就可以使用它来进行时间维度上构建指标分析.     在前面几篇中我们用到的hitogr ...

  9. OCR6:Custom Traineddata

    参考:https://groups.google.com/forum/#!msg/tesseract-ocr/MSYezIbckvs/kO1VoNKMDMQJ V4版本代码示例 : import py ...

  10. linux-ifconfig 查看没有IP

    ifconfig 查看没有IP,如图: 解决方法: 1.切换路径到 2.进入编辑ifcfg-ens33文件(文件名可能不同)模式 3.ONBOOT改为yes 4.点击ESC,输入:wq进行保存 5.输 ...