Given an arraynums, write a function to move all0's to the end of it while maintaining the relative order of the non-zero elements.

Note:

  1. You must do this in-place without making a copy of the array.
  2. Minimize the total number of operations.
class Solution {
    public void moveZeroes(int[] nums) {
        if(nums == null || nums.length <= 1)
            return;

        for(int i = 0; i < nums.length; i++) {
            if(nums[i] == 0) {
                int j = i;
                while(j < nums.length && nums[j] == 0)
                    j++;
                if(j == nums.length)
                    return;

                nums[i] = nums[j];
                nums[j] = 0;
            }
        }
    }
}

results matching ""

    No results matching ""