Skip to content
CodeShare
All problems

Move Zeroes

Shift every zero to the end while keeping the other numbers in order.

EasyArraysTwo pointers

Given an integer array nums, move all the 0s to the end while keeping the relative order of the non-zero numbers.

Do this in place: change nums directly instead of returning a new array. The tests check nums after your function finishes.

Examples

Example 1
Input
nums = [0, 1, 0, 3, 12]
Output
[1, 3, 12, 0, 0]
Why
The non-zero numbers keep their order: 1, 3, 12.
Example 2
Input
nums = [0]
Output
[0]

Constraints

  • 1 <= nums.length <= 10^4
  • -2^31 <= nums[i] <= 2^31 - 1