LeetCode 722. Remove Comments
原题链接在这里: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的更多相关文章
- 【LeetCode】722. Remove Comments 解题报告(Python)
[LeetCode]722. Remove Comments 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/remove-c ...
- 【leetcode】722. Remove Comments
题目如下: Given a C++ program, remove comments from it. The program source is an array where source[i] i ...
- LC 722. Remove Comments
Given a C++ program, remove comments from it. The program source is an array where source[i] is the ...
- 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 ...
- LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>
LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...
- 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++> 给出排序好的 ...
- [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% ...
随机推荐
- Mysql 表分区分类
针对Mysql数据库,表分区类型简析. [1]表分区类型 (1)Range分区:按范围分区.按列值的范围区间进行分区存储:比如:id小于10存储在一个分区:id大于10小于20存储在另外一个分区: ( ...
- 3、Vue实例的属性
1.获取Vue实例的属性 2.data属性 每个Vue实例都会代理其data对象里所有的属性.如果实例创建之后添加或者更改属性,他不会触发视图更新. 这句话说了下面两件事情 1.每个Vue实例都会代理 ...
- C# VB .net读取识别条形码线性条码codabar
codabar是比较常见的条形码编码规则类型的一种.如何在C#,vb等.NET平台语言里实现快速准确读取codabar条形码呢?答案是使用SharpBarcode! SharpBarcode是C#快速 ...
- Django--模型层进阶
目录 QuerySet对象 可切片 可迭代 惰性查询 缓存机制 何时查询集不会被缓存? exists()与iterator()方法 exists() iterator() 中介模型 查询优化 表数据 ...
- Windows中常用工具
护眼软件 f.lux https://justgetflux.com/ Typora https://www.typora.io/ Markdown工具,小巧,方便. Snipaste https:/ ...
- css, js 项目练习之网页换肤
首先,该练习参考自:https://www.jianshu.com/p/2961d9c317a3 我就直接上代码了(颜色可以自己调). HTML: <nav> <li>< ...
- Android实现二维码扫描功能
1.效果预览 先上图展示效果(模拟器没有摄像头,录出来效果不好,将就看) 2.集成步骤 1.拷贝本项目demo中的com.google.zxing5个包引入到自己的项目中. 2.拷贝本项目demo中的 ...
- Python 常用语句
条件语句 a=input("请输入数字a的值:\n") a=int(a) #从控制台接收到的都是字符串类型,需要转换 if a==0: #也可以写成if(a==0): print( ...
- Java面试通关要点 汇总集【最终版】
本文转载自公众号:服务端思维,阅读大约需要7分钟.梁兄的知识储备很丰富,组织的知识星球里也是干货十足,平常还会有技术研习等活动,欢迎关注. 首先,声明下,以下知识点并非阿里的面试题.这里,笔者结合自己 ...
- Kafka Streams开发入门(4)
背景 上一篇演示了filter操作算子的用法.今天展示一下如何根据不同的条件谓词(Predicate)将一个消息流实时地进行分流,划分成多个新的消息流,即所谓的流split.有的时候我们想要对消息流中 ...