Given an array nums
, write a function to move all 0
's to the end of it while maintaining the relative order of the non-zero elements.
Input: [0,1,0,3,12] Output: [1,3,12,0,0]
- You must do this in-place without making a copy of the array.
- Minimize the total number of operations.
impl Solution {
pub fn move_zeroes(nums: &mut Vec<i32>) {
let mut i = 0;
for j in 0..nums.len() {
if nums[j] != 0 {
nums[i] = nums[j];
i += 1;
}
}
for j in i..nums.len() {
nums[j] = 0;
}
}
}
impl Solution {
pub fn move_zeroes(nums: &mut Vec<i32>) {
let mut i = 0;
for _ in 0..nums.len() {
if nums[i] == 0 {
nums.remove(i);
nums.push(0);
} else {
i += 1;
}
}
}
}