Skip to content

Commit

Permalink
Create 05-apply-operations-to-an-array.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
xhofe committed Jun 6, 2023
1 parent 560c9e6 commit d884f93
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions 2023/06/05-apply-operations-to-an-array.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
impl Solution {
pub fn apply_operations(mut nums: Vec<i32>) -> Vec<i32> {
(0..nums.len() - 1).for_each(|i| {
if nums[i] == nums[i + 1] {
nums[i] *= 2;
nums[i + 1] = 0;
}
});
let (mut l, r): (Vec<_>, Vec<_>) = nums.into_iter().partition(|&x| x != 0);
l.extend(r);
l
}
}

0 comments on commit d884f93

Please sign in to comment.