-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
/
Copy pathSolution.rs
37 lines (36 loc) · 1.15 KB
/
Solution.rs
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
26
27
28
29
30
31
32
33
34
35
36
37
impl Solution {
pub fn remove_comments(source: Vec<String>) -> Vec<String> {
let mut ans: Vec<String> = Vec::new();
let mut t: Vec<String> = Vec::new();
let mut blockComment = false;
for s in &source {
let m = s.len();
let mut i = 0;
while i < m {
if blockComment {
if i + 1 < m && &s[i..i + 2] == "*/" {
blockComment = false;
i += 2;
} else {
i += 1;
}
} else {
if i + 1 < m && &s[i..i + 2] == "/*" {
blockComment = true;
i += 2;
} else if i + 1 < m && &s[i..i + 2] == "//" {
break;
} else {
t.push(s.chars().nth(i).unwrap().to_string());
i += 1;
}
}
}
if !blockComment && !t.is_empty() {
ans.push(t.join(""));
t.clear();
}
}
ans
}
}