Java accepted simple solution


  • 1
    Y
    public class Solution {
    public void moveZeroes(int[] nums) {
       if(nums.length==0) return;
       for(int i=0;i<nums.length;i++) {
           if(nums[i]==0) {
               int j=1;
               while((i+j)<nums.length&&nums[i+j]==0) j++;
               if((i+j)==nums.length) return;
               else {nums[i]=nums[i+j];
               nums[i+j]=0;
               }
               
           }
       }
       return;
    }
    

    }
    Just swap zeroes with the nearest non-zeroes


Log in to reply
 

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