1089. Duplicate Zeros

Given a fixed length array arr of integers, duplicate each occurrence of zero, shifting the remaining elements to the right.

Note that elements beyond the length of the original array are not written.

Do the above modifications to the input array in place, do not return anything from your function.

 

Example 1:

Input: [1,0,2,3,0,4,5,0]
Output: null
Explanation: After calling your function, the input array is modified to: [1,0,0,2,3,0,0,4]

Example 2:

Input: [1,2,3]
Output: null
Explanation: After calling your function, the input array is modified to: [1,2,3]

 

Note:

  1. 1 <= arr.length <= 10000
  2. 0 <= arr[i] <= 9

Rust Solution

struct Solution;

impl Solution {
    fn duplicate_zeros(arr: &mut Vec<i32>) {
        let n = arr.len();
        let m = arr.iter().filter(|&x| *x == 0).count();
        let mut i = n - 1;
        let mut j = m + n - 1;
        while i > 0 {
            if arr[i] != 0 {
                if j < n {
                    arr[j] = arr[i];
                }
                i -= 1;
            } else {
                if j < n {
                    arr[j] = 0;
                }
                i -= 1;
                j -= 1;
                if j < n {
                    arr[j] = 0;
                }
            }
            j -= 1;
        }
        if arr[i] != 0 {
            arr[j] = arr[i];
        } else {
            arr[j] = 0;
            j -= 1;
            arr[j] = 0;
        }
    }
}

#[test]
fn test() {
    let mut arr = vec![1, 0, 2, 3, 0, 4, 5, 0];
    let res = vec![1, 0, 0, 2, 3, 0, 0, 4];
    Solution::duplicate_zeros(&mut arr);
    assert_eq!(arr, res);
}

Having problems with this solution? Click here to submit an issue on github.