leetcode刷题报告 之hrt篇 oa leetcode 722 Remove Comments python
Given a C++ program, remove comments from it. The program source is an array of strings source where source[i] is the ith 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 the 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.
For example, 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.
這題 我愿意稱之為 字符串頂流題
這題看著不難,但坑很多。
先來弄清楚一些情況比如:
// abcdef /*
這里的/*是可以忽略的 就是一旦出現 “//“ 那//之后的內容都忽略
/* abc // 這也是一樣 // 是可以忽略的,一旦有/* 后面的內容都是comment直到 */出現
a/* hahaha **/b
或者
a/ * haha
haha */ b
的結果都是 ab
也就是說注釋符號前后的 內容我們是要保留的
同理
abc // eve
這里保留的是 abc
還有其他要素,要素過多
1 不要有typo, / * , * / 是左斜杠
2 這里直接去一整行判斷,比如 if /* in sentence,是不行的因為 可能一行可能同時含有 //, /* 和 * /
3 這里最棘手的就是 /* 因為受他影響, 一個普通的行也會被忽略,他是一個超越單行句子之外的影響因素,自然想到要設立一個flag來記錄是不是正被/*包裹
并且在遍歷每個字符串的時候 都要給這個flag做一次判斷
總結
以上是生活随笔為你收集整理的leetcode刷题报告 之hrt篇 oa leetcode 722 Remove Comments python的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 1-7 Burpsuite 爬虫介绍
- 下一篇: 程序员面试注意几点就够了