import org.junit.Test;

import static org.junit.Assert.*;

/**
 * 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.
 *
 * For example, given nums = [0, 1, 0, 3, 12], after calling your function, nums should be [1, 3, 12, 0, 0].
 *
 * Note:
 * You must do this in-place without making a copy of the array.
 * Minimize the total number of operations.
 *
 * Tags: Array, Two Pointers
 *
 * Similar Problems: (E) Remove Element
 */
public class MoveZeroes {

    /**
     * move all positive to the front
     * add trailing zeroes
     */
    public void moveZeroes(int[] nums) {
        if (nums == null || nums.length == 0) return;
        int count = 0;
        for (int n : nums) {
            if (n != 0) {
                nums[count] = n;
                count++;
            }
        }
        for (; count < nums.length; count++) {
            nums[count] = 0;
        }
    }

    @Test
    public void tests() {
        MoveZeroes mz = new MoveZeroes();
        int[] nums = null;
        // null -> null
        mz.moveZeroes(nums);
        assertNull(nums);
        // empty -> empty
        nums = new int[]{};
        mz.moveZeroes(nums);
        assertEquals(nums.length, 0);
        assertNotNull(nums);
        // [1] -> [1]
        nums = new int[]{1};
        mz.moveZeroes(nums);
        assertArrayEquals(nums, new int[]{1});
        // no zero, [1, 2, 3] -> [1, 2, 3]
        nums = new int[]{1, 2, 3};
        mz.moveZeroes(nums);
        assertArrayEquals(nums, new int[]{1, 2, 3});
        // all zeroes, [0, 0, 0] -> [0, 0, 0]
        nums = new int[]{0, 0, 0};
        mz.moveZeroes(nums);
        assertArrayEquals(nums, new int[]{0, 0, 0});
        // normal case, [0, 1, 0, 3, 12] -> [1, 3, 12, 0, 0]
        nums = new int[]{0, 1, 0, 3, 12};
        mz.moveZeroes(nums);
        assertArrayEquals(nums, new int[]{1, 3, 12, 0, 0});
    }
}

@@ -1,12 +1,62 @@
+package com.freetymekiyan.algorithms.level.easy;%0A%0A
 import org.j
@@ -98,17 +98,167 @@
 .Assert.
-*
+assertArrayEquals;%0Aimport static org.junit.Assert.assertEquals;%0Aimport static org.junit.Assert.assertNotNull;%0Aimport static org.junit.Assert.assertNull
 ;%0A%0A/**%0A 
@@ -394,24 +394,28 @@
 elements.%0A *
+ %3Cp%3E
 %0A * For exam
@@ -510,24 +510,28 @@
 2, 0, 0%5D.%0A *
+ %3Cp%3E
 %0A * Note:%0A *
@@ -639,16 +639,57 @@
 ions.%0A *
+ %3Cp%3E%0A * Company Tags: Bloomberg, Facebook
 %0A * Tags
@@ -714,11 +714,8 @@
 ers%0A
- *%0A
  * S
@@ -796,17 +796,166 @@
 %0A     * 
-m
+Two pointers.%0A     * One pointer goes through the array and find positive numbers.%0A     * The other tracks the next position to be filled up.%0A     * M
 ove all 
@@ -963,16 +963,24 @@
 ositive 
+numbers 
 to the f
@@ -983,23 +983,42 @@
 he front
+ of the array.
 %0A     *
+ Then
  add tra
@@ -1029,16 +1029,23 @@
 g zeroes
+ after.
 %0A     */
@@ -1136,15 +1136,39 @@
  0) 
-return;
+%7B%0A            return;%0A        %7D
 %0A   
@@ -1417,16 +1417,483 @@
 %0A    %7D%0A%0A
+    /**%0A     * One loop.%0A     * Go through the array.%0A     * If current number is not zero, swap it to the front.%0A     * Where we track the swap position with another pointer.%0A     */%0A    public void moveZeroesB(int%5B%5D nums) %7B%0A        int cur = 0;%0A        for (int i = 0; i %3C nums.length; ++i) %7B%0A            if (nums%5Bi%5D != 0) %7B%0A                int temp = nums%5Bcur%5D;%0A                nums%5Bcur++%5D = nums%5Bi%5D;%0A                nums%5Bi%5D = temp;%0A            %7D%0A        %7D%0A    %7D%0A%0A
     @Tes

update remove zeros