JavaScript BEAT 97 %


  • 0
    D
    var moveZeroes = function(nums) {
        var i = 0;  // pointer to 0
        var j = 0;  // pointer to non-0
            
        while(i<nums.length && j<nums.length ){
                
            if(nums[i]!==0){
                i++;
            }else if(nums[j]===0){
                j++;
            }else{
                if(i<j){
                    var temp = nums[i];
                    nums[i] = nums[j];
                    nums[j] = temp;
                }else{
                    j++;
                }
            }
                
        }
    };
    

Log in to reply
 

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