Solution that is different from most votes without copy paste zero at the end


  • 6
    public void moveZeroes(int[] nums) {
    
    	if (nums == null || nums.length == 0) {
    		return;
    	}
    
    	int count = 0;
    	for (int i = 0; i < nums.length; i++) {
    		if (nums[i] == 0) {
    			count++;
    		} else {
    			int temp = nums[i];
    			nums[i] = nums[i - count];
    			nums[i - count] = temp;
    		}
    	}
    }

  • 0

    This answer is great!


  • 0

    Hope Lily can see it.


Log in to reply
 

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