ntree.ai

Move Zeroes

mediumArraysTwo pointers

Given an array nums, move every 0 to the end while keeping the order of the non-zero elements. Return the array.

Example 1:

Input:  nums = [0, 1, 0, 3, 12]
Output: [1, 3, 12, 0, 0]
Explanation: 1, 3 and 12 keep their order; both zeroes fall to the back.

Example 2:

Input:  nums = [1, 2, 3]
Output: [1, 2, 3]
Explanation: no zeroes, nothing moves.

Constraints:

  • 1 <= len(nums) <= 10^4
  • -2^31 <= nums[i] <= 2^31 - 1