JAVA(0ms)beats 89.99%,easily understand.


  • 0

    public class Solution {
    public void moveZeroes(int[] nums) {

        int end=0;
        int front=0;
        while(end<nums.length && nums[end]!=0){                                 
            end++;                       //find the first position of zero;
        }
        front=end+1;                  //from the position of first zero, to swap the numbers.
        while(front<nums.length){
            if(nums[front]!=0){      //find the first non-zero number.
                nums[end]=nums[front];
                end++;
                nums[front]=0;
            }
            front++;
        }
        
    }
    

    }


Log in to reply
 

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