DEV Community

Kunal Tanti
Kunal Tanti

Posted on

30 day leetcoding challenge - Day 4 - Move Zeros

//One pass Time O(n) space O(1)
 public void MoveZeroes(int[] nums) {

    if(nums == null || nums.Length == 0)
    {
        return;
    }

    int temp = 0;                
    int nonZeroIndex = 0;
    for (int i = 0; i < nums.Length; i++)
    {
        if (nums[i] != 0)
        {
            temp = nums[i];
            nums[i] = 0;
            nums[nonZeroIndex] = temp;                
            nonZeroIndex++;
        }
    }


}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)