-
Notifications
You must be signed in to change notification settings - Fork 0
/
apply-operations-to-an-array.rs
48 lines (43 loc) · 1.04 KB
/
apply-operations-to-an-array.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
38
39
40
41
42
43
44
45
46
47
48
struct Solution;
impl Solution {
pub fn apply_operations(mut nums: Vec<i32>) -> Vec<i32> {
for i in 0..nums.len() - 1 {
if nums[i] == nums[i + 1] {
nums[i] *= 2;
nums[i + 1] = 0;
}
}
let (mut i, mut j) = (0, 1);
while j < nums.len() {
if nums[i] == 0 {
if nums[j] == 0 {
j += 1;
continue;
}
nums.swap(i, j);
}
i += 1;
j += 1;
}
nums
}
}
fn main() {
println!("{:?}", Solution::apply_operations(vec![0, 1]));
println!(
"{:?}",
Solution::apply_operations(vec![300, 126, 0, 0, 523, 523])
);
}
#[cfg(test)]
mod tests {
#[test]
fn test() {
use crate::Solution;
assert_eq!(Solution::apply_operations(vec![0, 1]), [1, 0]);
assert_eq!(
Solution::apply_operations(vec![300, 126, 0, 0, 523, 523]),
[300, 126, 1046, 0, 0, 0]
);
}
}