Simple two pointer Java solution


  • 1
    Z

    if(nums==null || nums.length==0){
    return;
    }

        int i = 0;
        int j = 0;
        
        while(i!=nums.length){
            if(nums[i]==0){
                i++;
            }else{
                if(i!=j){
                nums[j] = nums[i];
                nums[i] = 0;
                }
                i++;
                j++;
            }
        }

Log in to reply
 

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