1ms Java solution


  • 87
    V

    public class Solution {

    public void moveZeroes(int[] nums) {
    
        int j = 0;
        for(int i = 0; i < nums.length; i++) {
            if(nums[i] != 0) {
                int temp = nums[j];
                nums[j] = nums[i];
                nums[i] = temp;
                j++;
            }
        }
    }
    

    }


  • 0
    X

    NICE,GOOD JOB!


  • 0
    V

    Thank you....


  • 0
    T

    Oh my God. This idea is so concise. I have submitted a stupid solution.


  • 0
    V

    That happened to me like a 100 times. But that is the point in sharing answers; you keep improving.
    I think someone might be able to do it without even using the temp variable because nums[j] is always supposed to be 0. If you want to give it a shot....


  • 12
    W

    Inspired by your code, my implementation doesn't use a temp variable and avoids unnecessary swaps when nums has leading non-zero element.

    Idea - Set leftMostZeroIndex to 0. Iterate through the array, at each iteration i, if nums[i] != 0 and i > leftMostZeroIndex, replace the leftmost zero element nums[leftMostZeroIndex] with nums[i], and set nums[i] to 0.

    Note - i == leftMostZeroIndex happens when nums has leading non-zero elements, e.g., nums = {2, 1, 3, 0, 5, 0, 6}. In such a case, we don't perform any swap, and keep incrementing i and leftMostZeroIndex until i > leftMostZeroIndex.

    public void moveZeroes(int[] nums) {
        int leftMostZeroIndex = 0; // The index of the leftmost zero
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] != 0) {
                if (i > leftMostZeroIndex) { // i is larger than or equal to leftMostZeroIndex
                    nums[leftMostZeroIndex] = nums[i];
                    nums[i] = 0;
                }
    
                leftMostZeroIndex++;
            }
        }
    }

  • 0
    Y

    @Walkboy: Very elegant solution! Thanks!


  • 0
    A

    I just thought of this after reading the first solution. Why to store zeros in a temp value and shifting them! Brilliant!! :)


  • 0
    T

    this should be moved to the top.


  • 5
    Y

    This is a brilliant solution that not only applies for moving zeros, but also moving other numbers.
    eg. move any number smaller than 5 to the end, keeping other elements in relative order.
    [1, 2, 3, 4,5,6,7,8,9,10]

    After using this algorithm, it goes to [5,6,7,8,9,10,1,2,3,4], which also keeps relative order of numbers.


  • 0
    E
    This post is deleted!

  • 0

    @vshldc Got something similar :)

    public void moveZeroes(int[] nums) {
       for (int i = 0, j = 0; j < nums.length; j++)
          if (nums[j] != 0)
             swap(nums, i++, j);
    }
    
    private void swap(int[] arr, int i, int j) {
       int tmp = arr[i];
       arr[i] = arr[j];
       arr[j] = tmp;
    }
    

  • 1
    X
    public void moveZeroes(int[] nums) {
            int j=0;
            for(int i=0;i<nums.length;i++){
                if(nums[i] != 0){
                    if(i!=j){
                        int temp = nums[j];
                        nums[j] = nums[i];
                        nums[i] = temp;
                    }
                    j++;
                }
            }
        }
    

    I just added one if condition to reduce the number of swap operations


  • 0
    C

    very good solution!!!


  • 0
    G

    @vshldc well,good solution,i konw what you mean:)


  • 0
    H

    Eliminates unnecessary swaps if array has no zero in it:

        public void moveZeroes(int[] nums) {
            int toBeReplaced = -1;
            for (int i = 0; i < nums.length;i++) {
                if (nums[i] == 0) {
                    if (toBeReplaced == -1) {
                        toBeReplaced = i;
                    }
                } else if (toBeReplaced != -1) {
                    nums[toBeReplaced] = nums[i];
                    nums[i] = 0;
                    toBeReplaced++;
                }
            }
        }
    }
    ```

  • 0
    S

    Simple bubble sort idea, good!


  • 0
    V

    @vshldc Thanks to your good answer!


  • 0
    S

    My solution is similar to yours.

    public class Solution {
    public void moveZeroes(int[] nums) {
    int j =0;
    for(int index =0; index<nums.length; index++){
    if(nums[index]!=0){
    if(nums[j]==0){
    int tmp =nums[index];
    nums[index] = nums[j];
    nums[j] = tmp;
    }
    j++;
    }
    }

    }
    

    }


  • 0
    S

    @vshldc Good job!!


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.