Skip to content

Commit e0b250e

Browse files
committed
Add solution #722
1 parent 6ede739 commit e0b250e

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
617|[Merge Two Binary Trees](./0617-merge-two-binary-trees.js)|Easy|
2020
648|[Replace Words](./0648-replace-words.js)|Medium|
2121
686|[Repeated String Match](./0686-repeated-string-match.js)|Easy|
22+
722|[Remove Comments](./0722-remove-comments.js)|Medium|
2223
739|[Daily Temperatures](./0739-daily-temperatures.js)|Medium|
2324
791|[Custom Sort String](./0791-custom-sort-string.js)|Medium|
2425
804|[Unique Morse Code Words](./0804-unique-morse-code-words.js)|Easy|

solutions/0722-remove-comments.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* 722. Remove Comments
3+
* https://leetcode.com/problems/remove-comments/
4+
* Difficulty: Medium
5+
*
6+
* Given a C++ program, remove comments from it. The program source is an array where source[i] is the i-th
7+
* line of the source code. This represents the result of splitting the original source code string by the
8+
* newline character \n.
9+
* In C++, there are two types of comments, line comments, and block comments.
10+
* The string // denotes a line comment, which represents that it and rest of the characters to the right
11+
* of it in the same line should be ignored.
12+
* The string /* denotes a block comment, which represents that all characters until the next
13+
* (non-overlapping) occurrence of should be ignored.
14+
* It is guaranteed that every open block comment will eventually be closed, so /* outside of a line or block
15+
* comment always starts a new comment.
16+
* Finally, implicit newline characters can be deleted by block comments.
17+
* After removing the comments from the source code, return the source code in the same format.
18+
*/
19+
20+
/**
21+
* @param {string[]} source
22+
* @return {string[]}
23+
*/
24+
var removeComments = function(source) {
25+
return source.join('\n').replace(/\/\*.*?\*\/|\/\/[^\n]+/gs, '').split(/\n/).filter(Boolean);
26+
};

0 commit comments

Comments
 (0)