日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

leetcode刷题报告 之hrt篇 oa leetcode 722 Remove Comments python

發布時間:2023/12/18 python 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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做一次判斷

class Solution:def removeComments(self, source: List[str]) -> List[str]:ans = []star = Falsecontent = ""for s in source:# if in /* state, the content need be connected with content before /*if not star:content = ""i = 0 while i < len(s):# all use if elif, every judgement should have a star flagif s[i:i+2] == "/*" and not star:i+=1# need add one to jump this comment signalstar = Trueelif s[i:i+2] == "//" and not star:i += 1breakelif s[i:i+2] == "*/" and star:star = Falsei += 1elif not star:content += s[i]i += 1if content!="" and not star:ans.append(content)return ans

總結

以上是生活随笔為你收集整理的leetcode刷题报告 之hrt篇 oa leetcode 722 Remove Comments python的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。