move-zeroes leetcode problem L283

less than 1 minute read

3sum problem

3sum leetcode

solution

class Solution {
    public void moveZeroes(int[] nums) {
        int idx = 0;
        for(int i=0;i<nums.length;i++){
            if(nums[i]!=0) nums[idx++] = nums[i]; 
        }
        for(int i=idx;i<nums.length;i++){
            nums[i] = 0;
        }
        return;
    }
}

For this problem,

we can loop through the arrayand move swap all the non 0 numbers in nums array to the idx position. We can then change the values to zero starting from the last of the idx value. This will be a complexity of O(n) time | O(1) space

Leave a comment