LC 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.
Runtime: 4 ms, faster than 96.80% of Java online submissions for Remove Comments.
class Solution {
public List<String> removeComments(String[] source) {
boolean multiline_comment = false;
List<String> ret = new ArrayList<>();
StringBuilder sb = new StringBuilder();
for(String lines : source){
for(int i=; i<lines.length(); i++){
if(!multiline_comment && lines.charAt(i) == '/' && i+ < lines.length() && lines.charAt(i+) == '/'){
break;
}else if(!multiline_comment && lines.charAt(i) == '/' && i+ < lines.length() && lines.charAt(i+) == '*'){
i++;
multiline_comment = true;
}else if(multiline_comment && lines.charAt(i) == '*' && i+ < lines.length() && lines.charAt(i+) == '/'){
i++;
multiline_comment = false;
}else if(!multiline_comment){
sb.append(lines.charAt(i));
}
}
if(!multiline_comment){
if(sb.length() != ) ret.add(sb.toString());
sb = new StringBuilder();
}
}
return ret;
}
}
LC 722. Remove Comments的更多相关文章
- 【LeetCode】722. Remove Comments 解题报告(Python)
[LeetCode]722. Remove Comments 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/remove-c ...
- LeetCode 722. Remove Comments
原题链接在这里:https://leetcode.com/problems/remove-comments/ 题目: Given a C++ program, remove comments from ...
- 【leetcode】722. Remove Comments
题目如下: Given a C++ program, remove comments from it. The program source is an array where source[i] i ...
- 722. Remove Comments
class Solution { public: vector<string> removeComments(vector<string>& source) { vec ...
- [LeetCode] Remove Comments 移除注释
Given a C++ program, remove comments from it. The program source is an array where source[i] is the ...
- [Swift]LeetCode722. 删除注释 | Remove Comments
Given a C++ program, remove comments from it. The program source is an array where source[i] is the ...
- LC 660. Remove 9 【lock, hard】
Start from integer 1, remove any integer that contains 9 such as 9, 19, 29... So now, you will have ...
- [LC] 82. Remove Adjacent Repeated Characters IV
Repeatedly remove all adjacent, repeated characters in a given string from left to right. No adjacen ...
- [LC] 203. Remove Linked List Elements
Remove all elements from a linked list of integers that have value val. Example: Input: 1->2-> ...
随机推荐
- 第七章、Ajango自带auth模块
目录 第七章.Ajango自带auth模块 一.什么是auth auth是django自带的用户认证模块 二.auth模块的常用方法 三.拓展默认的auth_user表 第七章.Ajango自带aut ...
- linux 使用 rz 上传和 sz下载 命令
linux系统 root权限 lrzsz安装包 ①.在线安装-执行命令 yum install lrzsz 离线安装-需要提前准备好安装包 编译安装 root 账号登陆后,依次执行以下命令: tar ...
- Delphi 数组特性
- 在线p图网址
在线P图网址 如果你是简易的P图,不用那么麻烦的去下载安装Photoshop,可以使用以下网址在线编辑 https://www.uupoop.com/ps/?hmsr=ps_menu
- anaconda环境---ubuntu下重装
anaconda环境---ubuntu下重装 @wp20190312 为何重装? 配置一个环境,意外发现conda命令不好用了,提示“找不到conda模块”,整个conda虚拟环境中的工程项目无法使用 ...
- C++STL库常用函数用法
开学就要上OOP了.....感觉十分萌萌哒- -! 整理自<ACM程序设计>,本文为转载(原文地址) 迭代器(iterator) 个人理解就是把所有和迭代有关的东西给抽象出来的,不管是数组 ...
- 使用PyInstaller将.py文件打包并生成Windows下可执行的.exe文件
最近要使用Qt写一个简单的GUI程序,因此使用了PyQt5来加快开发,使用PyQt5生成可执行的程序时,在Windows操作系统下可以使用pyinstaller库将.py文件及其相关依赖生成为.exe ...
- NativeRenderingPlugin IOS
https://bitbucket.org/Unity-Technologies/graphicsdemos/src/77f014c12161e5c25d902e2c5697dd0c45ce3e35/ ...
- HTML中dl元素的高度问题
dl元素通常用来创建一个描述列表,但是在我使用的过程中发现了一个小问题. 定义及用法 在MDN中 <dl> 元素的定义是:一个包含术语定义以及描述的列表,通常用于展示词汇表或者元数据 (键 ...
- BZOJ 3289: Mato的文件管理 (区间查询逆序对)
这道题就是不要求强制在线的 BZOJ 3744 Gty的妹子序列 所以说离线做法有莫队,在线做法见上面连接. 这里贴出常数巨大O(nnlogn)O(n\sqrt nlogn)O(nnlogn)分块+ ...