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
sourceis 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% ...
随机推荐
- Dart面向对象编程(一)
基本内容概述: 类与对象: 计算属性: void main(){ var rect = new Rectangle(); rect.width = 20; rect.height = 10; prin ...
- Java笔记_静态变量和实例变量的区别
这里简单做一下笔记,区分Java全局变量里的静态变量与实例变量. 1.Java里的全局变量 首先了解Java里的全局变量,也叫成员变量. 特点: (1).一个类中既不在方法体内,也不在程序块内定义的变 ...
- JAVAWEB之增删改查
青年志愿者服务网(20分) 1.项目需求: 为了适应社会主义市场经济发展的需要,推动青年志愿服务体系和多层次社会保障体系的建立和完善,促进青年健康成长,石家庄铁道大学急需建设青年志愿者服务网,推进 ...
- C# vb .net图像合成-合成自定义路径
在.net中,如何简单快捷地实现图像合成呢,比如合成文字,合成艺术字,多张图片叠加合成等等?答案是调用SharpImage!专业图像特效滤镜和合成类库.下面开始演示关键代码,您也可以在文末下载全部源码 ...
- MySQL5.6.17 绿色版 安装配置
安装篇: 下载完成之后,用解压工具解压到没有中文.空格的文件夹下,解压后的显示如图: 个人建议把解压后的文件夹重命名,如果有中文去掉中文,便于自己理解使用,如图: 打开重命名之后的文件夹,找到mysq ...
- web技术栈开发原生应用-多端共用一套代码
weex: vuejs开发原生应用 nativescript: vuejs开发原生应用 ReactNative = reactjs开发原生应用 ionic = angularjs 开发原生应用
- ElasticSearch 中文分词搜索环境搭建
ElasticSearch 是强大的搜索工具,并且是ELK套件的重要组成部分 好记性不如乱笔头,这次是在windows环境下搭建es中文分词搜索测试环境,步骤如下 1.安装jdk1.8,配置好环境变量 ...
- 面试题:什么叫B树
B是balanced的意思 是在二叉查找树 树的基础上增加了平衡的性质 导致它不是 二叉树了,变成了多叉树,但是叉几路又有了限制 否则怎么平衡呢 一棵m阶B树(balanced tree o ...
- Ubuntu下映射网络驱动器
Ubuntu下映射网络驱动器https://www.linuxidc.com/Linux/2013-07/86928.htm linux下samba访问路径: smb://192.168.1.111/ ...
- idea中导入Maven项目,但是没有Maven Dependencies,且没有pom
1. 可以复制其他项目的类似的pom.xml文件复制到当前项目下 2. 导入此项目为maven项目 3. 配置相应的maven环境 4. 如果此时的Application.class中的main方法没 ...