Easy
Move All Zeroes to the End
- Time limit
- 1 s
- Memory limit
- 128 MB
- Estimated
- 15 min
- Two Pointers
Description
Given an array of integers, move every zero to the end of the array while preserving the relative order of all non-zero elements.
The result must have the same length as the input and contain exactly the same elements. Only the positions of the zeroes may change.
For example, [0, 1, 0, 3, 12] becomes [1, 3, 12, 0, 0].
Implement moveZeroes(nums) to return the rearranged array.
Constraints
1 <= nums.length <= 1000 -1000000 <= nums[i] <= 1000000 The relative order of non-zero elements must remain unchanged.
Sample cases
3Sample 1
Input
nums = [0,1,0,3,12]
Expected output
[1,3,12,0,0]
Sample 2
Input
nums = [1,2,3]
Expected output
[1,2,3]
Skills and languages
Patterns
- Two Pointers
Languages
- Python 3
Solve this problem
Create a free account to write, run and submit your solution.
- Write Python or Java in the browser, with your draft saved as you type
- Run your code on the examples as often as you like
- Submit to a sandboxed judge and see exactly which case failed
- Ask the AI mentor for a hint without being handed the answer