Java one for-loop


  • 0
    D
    public void moveZeroes(int[] nums) {
            int zc = -1;
            int count = 0;
            for (int i = 0; i < nums.length; i++) {
                if (nums[i] == 0) {
                    zc = i;
                    count++;
                } else if (zc != -1) {
                    nums[zc - (count > 1 ? count - 1 : 0)] = nums[i];
                    nums[i] = 0;
                    zc = i;
                }
            }
        }
    

Log in to reply
 

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