Simple solution with minimum operations


  • 0
    R
    public class Solution {
        public void moveZeroes(int[] nums) {
            int writeLocation = 0;
            for (int i = 0; i < nums.length; i++) {
                if (nums[i] != 0) {
                    if (i != writeLocation) {
                        nums[writeLocation] = nums[i];
                    }
                    writeLocation++;
                }
            }
            Arrays.fill(nums, writeLocation, nums.length, 0);
        }
    }
    

Log in to reply
 

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