Skip to content

Commit 2c37140

Browse files
committed
src/bin/move-zeroes.rs
1 parent 493a4e1 commit 2c37140

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

src/bin/move-zeroes.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
fn main() {
2+
let mut v = vec![0, 1, 0, 3, 12];
3+
Solution::move_zeroes(&mut v);
4+
println!("{:?}", v);
5+
}
6+
7+
struct Solution;
8+
9+
impl Solution {
10+
pub fn move_zeroes(nums: &mut Vec<i32>) {
11+
let (mut i, mut j) = (0, 0);
12+
13+
while j < nums.len() {
14+
match (nums[i], nums[j]) {
15+
(0, 0) => {
16+
j += 1;
17+
}
18+
(_, 0) => {
19+
j += 1;
20+
}
21+
(0, _) => {
22+
nums.swap(i, j);
23+
i += 1;
24+
}
25+
_ => {
26+
i += 1;
27+
j += 1;
28+
}
29+
}
30+
}
31+
}
32+
}

0 commit comments

Comments
 (0)