My Java solution track number of zero


  • 0
    S

    public class Solution {

    public void moveZeroes(int[] nums) {
        int numOfZero = 0;
        for (int i = 0 ; i < nums.length ; i++) {
            if(nums[i] == 0) {
                numOfZero += 1 ;
                continue;
            }
            if(numOfZero > 0) {
                nums[i-numOfZero] = nums[i];
            }
        }
        while(numOfZero > 0) {
            nums[nums.length - numOfZero] = 0;
            numOfZero--;
        }
    
    }
    

    }


Log in to reply
 

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