Move Zeroes
EasyTwo Pointers · Paypal
Given an integer array nums, move every 0 to the end of the array while keeping the non-zero values in their original relative order.
Modify nums in place without making a copy of the array. Do not return anything; the grader checks the contents of nums after your function finishes.
Example 1
- Input:
- nums = [4,0,0,-2,7,0,1]
- Output:
- [4,-2,7,1,0,0,0]
- Explanation
- The non-zero values 4, -2, 7, 1 keep their original order at the front, and the three zeroes fill the last three slots.
Example 2
- Input:
- nums = [0,0,9]
- Output:
- [9,0,0]
- Explanation
- The 9 moves to the front and the two zeroes follow it.
Example 3
- Input:
- nums = [3,8]
- Output:
- [3,8]
- Explanation
- There are no zeroes, so the array stays as it is.
Constraints
1 <= nums.length <= 10^4-2^31 <= nums[i] <= 2^31 - 1