Move Zeroes
You get an array of numbers. Move every zero to the end and keep the other numbers in the order they were in. Do it inside the same array, without a second one. Our example: [0, 4, 0, 0, 7, 2] → [4, 7, 2, 0, 0, 0].
- input
nums: integers, some of them may be0.- returns
- Nothing: change
numsin place so that all the zeros are at the end and the other numbers keep their order before them. Do not copy the array. - constraints
- 1 ≤ nums.length ≤ 10⁴
- −2³¹ ≤ nums[i] ≤ 2³¹ − 1
- examples
- Open a test in the tests panel: it shows the input and the expected answer.
/**
* @param {number[]} nums
* @return {void} Do not return anything, modify nums in-place instead.
*/
function moveZeroes(nums) {
}
Ctrl ↵
format
reset
saved in this browser
Stuck?
See the solution run step by step on our example, then why it works and its complexity.
Next: more two pointers problems
- 125Valid PalindromeEasysolve
- 26Remove Duplicates from Sorted ArrayEasysolve
- 88Merge Sorted ArrayEasysolve
- 27Remove ElementEasysolve
- 392Is SubsequenceEasysolve
- 167Two Sum II - Input Array Is SortedMediumsolve
- 153SumMediumsolve
- 11Container With Most WaterMediumsolve
- 80Remove Duplicates from Sorted Array IIMediumsolve
- 189Rotate ArrayMediumsolve
- 151Reverse Words in a StringMediumsolve
- 5Longest Palindromic SubstringMediumsolve
- 42Trapping Rain WaterHardsolve
The problem and its statement belong to LeetCode. Here are our own retelling, example and solution. Patterns Hero is not affiliated with LeetCode.