207. Move Zeroes

Easy
In-Place Compaction
Array

Problem

Given an integer array nums, move all zeroes to the end while maintaining the relative order of all non-zero elements. Return the transformed array.

Examples

Example 1

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

Example 2

Input: [0]
Output: [0]

Example 3

Input: [1,2,3]
Output: [1,2,3]
Constraints

1 <= nums.length <= 100000

-1000000000 <= nums[i] <= 1000000000

Hints

?? Write every non-zero value into the next available position.

?? Fill the remaining suffix with zeroes.

Expected Complexity
Time: O(n)
Space: O(1)
Follow-up

Can you minimize the number of writes?

Practice Notes

Attempts: 0

Time spent: 0min

Hints used: 0

00:00
Loading...
Case 1
Case 2
Case 3
Input
[0,1,0,3,12]
Expected
[1,3,12,0,0]