Skip to content

Commit 48da787

Browse files
authored
feat: add rust solution to lc problem: No.0722 (doocs#1358)
1 parent bc7f7a9 commit 48da787

File tree

4 files changed

+122
-1
lines changed

4 files changed

+122
-1
lines changed

solution/0400-0499/0470.Implement Rand10() Using Rand7()/Solution.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@
1515
}
1616
}
1717
}
18-
}
18+
}

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,48 @@ function removeComments(source: string[]): string[] {
285285
}
286286
```
287287

288+
### **Rust**
289+
290+
```rust
291+
impl Solution {
292+
pub fn remove_comments(source: Vec<String>) -> Vec<String> {
293+
let mut ans: Vec<String> = Vec::new();
294+
let mut t: Vec<String> = Vec::new();
295+
let mut blockComment = false;
296+
297+
for s in &source {
298+
let m = s.len();
299+
let mut i = 0;
300+
while i < m {
301+
if blockComment {
302+
if i + 1 < m && &s[i..i + 2] == "*/" {
303+
blockComment = false;
304+
i += 2;
305+
} else {
306+
i += 1;
307+
}
308+
} else {
309+
if i + 1 < m && &s[i..i + 2] == "/*" {
310+
blockComment = true;
311+
i += 2;
312+
} else if i + 1 < m && &s[i..i + 2] == "//" {
313+
break;
314+
} else {
315+
t.push(s.chars().nth(i).unwrap().to_string());
316+
i += 1;
317+
}
318+
}
319+
}
320+
if !blockComment && !t.is_empty() {
321+
ans.push(t.join(""));
322+
t.clear();
323+
}
324+
}
325+
ans
326+
}
327+
}
328+
```
329+
288330
### **...**
289331

290332
```

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,48 @@ function removeComments(source: string[]): string[] {
261261
}
262262
```
263263

264+
### **Rust**
265+
266+
```rust
267+
impl Solution {
268+
pub fn remove_comments(source: Vec<String>) -> Vec<String> {
269+
let mut ans: Vec<String> = Vec::new();
270+
let mut t: Vec<String> = Vec::new();
271+
let mut blockComment = false;
272+
273+
for s in &source {
274+
let m = s.len();
275+
let mut i = 0;
276+
while i < m {
277+
if blockComment {
278+
if i + 1 < m && &s[i..i + 2] == "*/" {
279+
blockComment = false;
280+
i += 2;
281+
} else {
282+
i += 1;
283+
}
284+
} else {
285+
if i + 1 < m && &s[i..i + 2] == "/*" {
286+
blockComment = true;
287+
i += 2;
288+
} else if i + 1 < m && &s[i..i + 2] == "//" {
289+
break;
290+
} else {
291+
t.push(s.chars().nth(i).unwrap().to_string());
292+
i += 1;
293+
}
294+
}
295+
}
296+
if !blockComment && !t.is_empty() {
297+
ans.push(t.join(""));
298+
t.clear();
299+
}
300+
}
301+
ans
302+
}
303+
}
304+
```
305+
264306
### **...**
265307

266308
```
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
impl Solution {
2+
pub fn remove_comments(source: Vec<String>) -> Vec<String> {
3+
let mut ans: Vec<String> = Vec::new();
4+
let mut t: Vec<String> = Vec::new();
5+
let mut blockComment = false;
6+
7+
for s in &source {
8+
let m = s.len();
9+
let mut i = 0;
10+
while i < m {
11+
if blockComment {
12+
if i + 1 < m && &s[i..i + 2] == "*/" {
13+
blockComment = false;
14+
i += 2;
15+
} else {
16+
i += 1;
17+
}
18+
} else {
19+
if i + 1 < m && &s[i..i + 2] == "/*" {
20+
blockComment = true;
21+
i += 2;
22+
} else if i + 1 < m && &s[i..i + 2] == "//" {
23+
break;
24+
} else {
25+
t.push(s.chars().nth(i).unwrap().to_string());
26+
i += 1;
27+
}
28+
}
29+
}
30+
if !blockComment && !t.is_empty() {
31+
ans.push(t.join(""));
32+
t.clear();
33+
}
34+
}
35+
ans
36+
}
37+
}

0 commit comments

Comments
 (0)