My easy java solution using two pointers


  • 0
    X
    public void moveZeroes(int[] nums) {
            if(null == nums || nums.length < 1){
                return;
            }
            int j = 0;           // to index the non-zero numbers
            for(int i = 0; i < nums.length; i++){
                if(nums[i] != 0){
                    int temp = nums[i];
                    nums[i] = 0;
                    nums[j++] = temp;
                }
            }
            
        }

Log in to reply
 

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