forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.py
25 lines (25 loc) · 874 Bytes
/
Solution.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Solution:
def removeComments(self, source: List[str]) -> List[str]:
ans = []
t = []
block_comment = False
for s in source:
i, m = 0, len(s)
while i < m:
if block_comment:
if i + 1 < m and s[i : i + 2] == "*/":
block_comment = False
i += 1
else:
if i + 1 < m and s[i : i + 2] == "/*":
block_comment = True
i += 1
elif i + 1 < m and s[i : i + 2] == "//":
break
else:
t.append(s[i])
i += 1
if not block_comment and t:
ans.append("".join(t))
t.clear()
return ans