#283

Move Zeroes

pupil · 340 · lc easy +22 · verified · 63.6% accepted · 19,284 likes · top 66%

play →

Description

Given an integer array nums, relocate all 0 elements to the end of the array while preserving the relative order of the non-zero elements.

This must be done in-place without making a copy of the array.

Example 1:

Input: nums = [0,1,0,3,12]
Output: [1,3,12,0,0]

Example 2:

Input: nums = [0]
Output: [0]

Code

1
2
3
4
5
6