O(n) in-place Java Solution


  • 3
    K
    public class Solution {
        public void moveZeroes(int[] nums) {
            int len = nums.length;
            int cur = -1;
            for(int i =0;i<len;i++)
                if(nums[i] != 0)
                    nums[++cur] = nums[i];
            
            for(int i=cur+1;i<len;i++)
                nums[i] = 0;
        }
    }

Log in to reply
 

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