Skip to content

Commit 05d82f8

Browse files
authored
feat: add solutions to lc problem: No.0722 (doocs#1356)
No.0722.Remove Comments
1 parent 6100246 commit 05d82f8

File tree

10 files changed

+492
-5
lines changed

10 files changed

+492
-5
lines changed

solution/0700-0799/0722.Remove Comments/README.md

+177-1
Original file line numberDiff line numberDiff line change
@@ -91,22 +91,198 @@ a = b + c;
9191

9292
<!-- 这里可写通用的实现逻辑 -->
9393

94+
**方法一:分情况讨论**
95+
96+
我们用一个变量 $blockComment$ 来表示当前是否处于块注释中,初始时 $blockComment$ 为 `false`;用一个变量 $t$ 来存储当前行的有效字符。
97+
98+
接下来,遍历每一行,分情况讨论:
99+
100+
如果当前处于块注释中,那么如果当前字符和下一个字符是 `'*/'`,说明块注释结束,我们将 $blockComment$ 置为 `false`,并且跳过这两个字符;否则,我们继续保持块注释状态,不做任何操作;
101+
102+
如果当前不处于块注释中,那么如果当前字符和下一个字符是 `'/*'`,说明块注释开始,我们将 $blockComment$ 置为 `true`,并且跳过这两个字符;如果当前字符和下一个字符是 `'//'`,那么说明行注释开始,我们直接退出当前行的遍历;否则,说明当前字符是有效字符,我们将其加入 $t$ 中;
103+
104+
遍历完当前行后,如果 $blockComment$ 为 `false`,并且 $t$ 不为空,说明当前行是有效行,我们将其加入答案数组中,并且清空 $t$。继续遍历下一行。
105+
106+
时间复杂度 $O(L)$,空间复杂度 $O(L)$,其中 $L$ 是源代码的总长度。
107+
94108
<!-- tabs:start -->
95109

96110
### **Python3**
97111

98112
<!-- 这里可写当前语言的特殊实现逻辑 -->
99113

100114
```python
101-
115+
class Solution:
116+
def removeComments(self, source: List[str]) -> List[str]:
117+
ans = []
118+
t = []
119+
block_comment = False
120+
for s in source:
121+
i, m = 0, len(s)
122+
while i < m:
123+
if block_comment:
124+
if i + 1 < m and s[i : i + 2] == "*/":
125+
block_comment = False
126+
i += 1
127+
else:
128+
if i + 1 < m and s[i : i + 2] == "/*":
129+
block_comment = True
130+
i += 1
131+
elif i + 1 < m and s[i : i + 2] == "//":
132+
break
133+
else:
134+
t.append(s[i])
135+
i += 1
136+
if not block_comment and t:
137+
ans.append("".join(t))
138+
t.clear()
139+
return ans
102140
```
103141

104142
### **Java**
105143

106144
<!-- 这里可写当前语言的特殊实现逻辑 -->
107145

108146
```java
147+
class Solution {
148+
public List<String> removeComments(String[] source) {
149+
List<String> ans = new ArrayList<>();
150+
StringBuilder sb = new StringBuilder();
151+
boolean blockComment = false;
152+
for (String s : source) {
153+
int m = s.length();
154+
for (int i = 0; i < m; ++i) {
155+
if (blockComment) {
156+
if (i + 1 < m && s.charAt(i) == '*' && s.charAt(i + 1) == '/') {
157+
blockComment = false;
158+
++i;
159+
}
160+
} else {
161+
if (i + 1 < m && s.charAt(i) == '/' && s.charAt(i + 1) == '*') {
162+
blockComment = true;
163+
++i;
164+
} else if (i + 1 < m && s.charAt(i) == '/' && s.charAt(i + 1) == '/') {
165+
break;
166+
} else {
167+
sb.append(s.charAt(i));
168+
}
169+
}
170+
}
171+
if (!blockComment && sb.length() > 0) {
172+
ans.add(sb.toString());
173+
sb.setLength(0);
174+
}
175+
}
176+
return ans;
177+
}
178+
}
179+
```
180+
181+
### **C++**
182+
183+
```cpp
184+
class Solution {
185+
public:
186+
vector<string> removeComments(vector<string>& source) {
187+
vector<string> ans;
188+
string t;
189+
bool blockComment = false;
190+
for (auto& s : source) {
191+
int m = s.size();
192+
for (int i = 0; i < m; ++i) {
193+
if (blockComment) {
194+
if (i + 1 < m && s[i] == '*' && s[i + 1] == '/') {
195+
blockComment = false;
196+
++i;
197+
}
198+
} else {
199+
if (i + 1 < m && s[i] == '/' && s[i + 1] == '*') {
200+
blockComment = true;
201+
++i;
202+
} else if (i + 1 < m && s[i] == '/' && s[i + 1] == '/') {
203+
break;
204+
} else {
205+
t.push_back(s[i]);
206+
}
207+
}
208+
}
209+
if (!blockComment && !t.empty()) {
210+
ans.emplace_back(t);
211+
t.clear();
212+
}
213+
}
214+
return ans;
215+
}
216+
};
217+
```
109218
219+
### **Go**
220+
221+
```go
222+
func removeComments(source []string) (ans []string) {
223+
t := []byte{}
224+
blockComment := false
225+
for _, s := range source {
226+
m := len(s)
227+
for i := 0; i < m; i++ {
228+
if blockComment {
229+
if i+1 < m && s[i] == '*' && s[i+1] == '/' {
230+
blockComment = false
231+
i++
232+
}
233+
} else {
234+
if i+1 < m && s[i] == '/' && s[i+1] == '*' {
235+
blockComment = true
236+
i++
237+
} else if i+1 < m && s[i] == '/' && s[i+1] == '/' {
238+
break
239+
} else {
240+
t = append(t, s[i])
241+
}
242+
}
243+
}
244+
if !blockComment && len(t) > 0 {
245+
ans = append(ans, string(t))
246+
t = []byte{}
247+
}
248+
}
249+
return
250+
}
251+
```
252+
253+
### **TypeScript**
254+
255+
```ts
256+
function removeComments(source: string[]): string[] {
257+
const ans: string[] = [];
258+
const t: string[] = [];
259+
let blockComment = false;
260+
for (const s of source) {
261+
const m = s.length;
262+
for (let i = 0; i < m; ++i) {
263+
if (blockComment) {
264+
if (i + 1 < m && s.slice(i, i + 2) === '*/') {
265+
blockComment = false;
266+
++i;
267+
}
268+
} else {
269+
if (i + 1 < m && s.slice(i, i + 2) === '/*') {
270+
blockComment = true;
271+
++i;
272+
} else if (i + 1 < m && s.slice(i, i + 2) === '//') {
273+
break;
274+
} else {
275+
t.push(s[i]);
276+
}
277+
}
278+
}
279+
if (!blockComment && t.length) {
280+
ans.push(t.join(''));
281+
t.length = 0;
282+
}
283+
}
284+
return ans;
285+
}
110286
```
111287

112288
### **...**

solution/0700-0799/0722.Remove Comments/README_EN.md

+163-1
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,175 @@ a = b + c;
9090
### **Python3**
9191

9292
```python
93-
93+
class Solution:
94+
def removeComments(self, source: List[str]) -> List[str]:
95+
ans = []
96+
t = []
97+
block_comment = False
98+
for s in source:
99+
i, m = 0, len(s)
100+
while i < m:
101+
if block_comment:
102+
if i + 1 < m and s[i : i + 2] == "*/":
103+
block_comment = False
104+
i += 1
105+
else:
106+
if i + 1 < m and s[i : i + 2] == "/*":
107+
block_comment = True
108+
i += 1
109+
elif i + 1 < m and s[i : i + 2] == "//":
110+
break
111+
else:
112+
t.append(s[i])
113+
i += 1
114+
if not block_comment and t:
115+
ans.append("".join(t))
116+
t.clear()
117+
return ans
94118
```
95119

96120
### **Java**
97121

98122
```java
123+
class Solution {
124+
public List<String> removeComments(String[] source) {
125+
List<String> ans = new ArrayList<>();
126+
StringBuilder sb = new StringBuilder();
127+
boolean blockComment = false;
128+
for (String s : source) {
129+
int m = s.length();
130+
for (int i = 0; i < m; ++i) {
131+
if (blockComment) {
132+
if (i + 1 < m && s.charAt(i) == '*' && s.charAt(i + 1) == '/') {
133+
blockComment = false;
134+
++i;
135+
}
136+
} else {
137+
if (i + 1 < m && s.charAt(i) == '/' && s.charAt(i + 1) == '*') {
138+
blockComment = true;
139+
++i;
140+
} else if (i + 1 < m && s.charAt(i) == '/' && s.charAt(i + 1) == '/') {
141+
break;
142+
} else {
143+
sb.append(s.charAt(i));
144+
}
145+
}
146+
}
147+
if (!blockComment && sb.length() > 0) {
148+
ans.add(sb.toString());
149+
sb.setLength(0);
150+
}
151+
}
152+
return ans;
153+
}
154+
}
155+
```
156+
157+
### **C++**
158+
159+
```cpp
160+
class Solution {
161+
public:
162+
vector<string> removeComments(vector<string>& source) {
163+
vector<string> ans;
164+
string t;
165+
bool blockComment = false;
166+
for (auto& s : source) {
167+
int m = s.size();
168+
for (int i = 0; i < m; ++i) {
169+
if (blockComment) {
170+
if (i + 1 < m && s[i] == '*' && s[i + 1] == '/') {
171+
blockComment = false;
172+
++i;
173+
}
174+
} else {
175+
if (i + 1 < m && s[i] == '/' && s[i + 1] == '*') {
176+
blockComment = true;
177+
++i;
178+
} else if (i + 1 < m && s[i] == '/' && s[i + 1] == '/') {
179+
break;
180+
} else {
181+
t.push_back(s[i]);
182+
}
183+
}
184+
}
185+
if (!blockComment && !t.empty()) {
186+
ans.emplace_back(t);
187+
t.clear();
188+
}
189+
}
190+
return ans;
191+
}
192+
};
193+
```
99194
195+
### **Go**
196+
197+
```go
198+
func removeComments(source []string) (ans []string) {
199+
t := []byte{}
200+
blockComment := false
201+
for _, s := range source {
202+
m := len(s)
203+
for i := 0; i < m; i++ {
204+
if blockComment {
205+
if i+1 < m && s[i] == '*' && s[i+1] == '/' {
206+
blockComment = false
207+
i++
208+
}
209+
} else {
210+
if i+1 < m && s[i] == '/' && s[i+1] == '*' {
211+
blockComment = true
212+
i++
213+
} else if i+1 < m && s[i] == '/' && s[i+1] == '/' {
214+
break
215+
} else {
216+
t = append(t, s[i])
217+
}
218+
}
219+
}
220+
if !blockComment && len(t) > 0 {
221+
ans = append(ans, string(t))
222+
t = []byte{}
223+
}
224+
}
225+
return
226+
}
227+
```
228+
229+
### **TypeScript**
230+
231+
```ts
232+
function removeComments(source: string[]): string[] {
233+
const ans: string[] = [];
234+
const t: string[] = [];
235+
let blockComment = false;
236+
for (const s of source) {
237+
const m = s.length;
238+
for (let i = 0; i < m; ++i) {
239+
if (blockComment) {
240+
if (i + 1 < m && s.slice(i, i + 2) === '*/') {
241+
blockComment = false;
242+
++i;
243+
}
244+
} else {
245+
if (i + 1 < m && s.slice(i, i + 2) === '/*') {
246+
blockComment = true;
247+
++i;
248+
} else if (i + 1 < m && s.slice(i, i + 2) === '//') {
249+
break;
250+
} else {
251+
t.push(s[i]);
252+
}
253+
}
254+
}
255+
if (!blockComment && t.length) {
256+
ans.push(t.join(''));
257+
t.length = 0;
258+
}
259+
}
260+
return ans;
261+
}
100262
```
101263

102264
### **...**

0 commit comments

Comments
 (0)