722. Remove Comments Medium
1/**
2 * [722] Remove Comments
3 *
4 * Given a C++ program, remove comments from it. The program source is an array of strings source 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'.
5 * In C++, there are two types of comments, line comments, and block comments.
6 *
7 * The string "//" denotes a line comment, which represents that it and the rest of the characters to the right of it in the same line should be ignored.
8 * 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.
9 *
10 * The first effective comment takes precedence over others.
11 *
12 * For example, if the string "//" occurs in a block comment, it is ignored.
13 * Similarly, if the string "/*" occurs in a line or block comment, it is also ignored.
14 *
15 * 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.
16 * There will be no control characters, single quote, or double quote characters.
17 *
18 * For example, source = "string s = "/* Not a comment. */";" will not be a test case.
19 *
20 * Also, nothing else such as defines or macros will interfere with the comments.
21 * 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.
22 * Finally, implicit newline characters can be deleted by block comments. Please see the examples below for details.
23 * After removing the comments from the source code, return the source code in the same format.
24 *
25 * Example 1:
26 *
27 * Input: source = ["/*Test program */", "int main()", "{ ", " // variable declaration ", "int a, b, c;", "/* This is a test", " multiline ", " comment for ", " testing */", "a = b + c;", "}"]
28 * Output: ["int main()","{ "," ","int a, b, c;","a = b + c;","}"]
29 * Explanation: The line by line code is visualized as below:
30 * /*Test program */
31 * int main()
32 * {
33 * // variable declaration
34 * int a, b, c;
35 * /* This is a test
36 * multiline
37 * comment for
38 * testing */
39 * a = b + c;
40 * }
41 * The string /* denotes a block comment, including line 1 and lines 6-9. The string // denotes line 4 as comments.
42 * The line by line output code is visualized as below:
43 * int main()
44 * {
45 *
46 * int a, b, c;
47 * a = b + c;
48 * }
49 *
50 * Example 2:
51 *
52 * Input: source = ["a/*comment", "line", "more_comment*/b"]
53 * Output: ["ab"]
54 * 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"].
55 *
56 *
57 * Constraints:
58 *
59 * 1 <= source.length <= 100
60 * 0 <= source[i].length <= 80
61 * source[i] consists of printable ASCII characters.
62 * Every open block comment is eventually closed.
63 * There are no single-quote or double-quote in the input.
64 *
65 */
66pub struct Solution {}
67
68// problem: https://leetcode.com/problems/remove-comments/
69// discuss: https://leetcode.com/problems/remove-comments/discuss/?currentPage=1&orderBy=most_votes&query=
70
71// submission codes start here
72
73impl Solution {
74 pub fn remove_comments(source: Vec<String>) -> Vec<String> {
75 vec![]
76 }
77}
78
79// submission codes end
80
81#[cfg(test)]
82mod tests {
83 use super::*;
84
85 #[test]
86 fn test_722() {
87 }
88}
89
Back
© 2025 bowen.ge All Rights Reserved.