Move Zeroes
Given an integer array nums, move all 0s to the end of the array while keeping the relative order of the non-zero elements. You must do this in-place without making a copy of the array.
Open official problem prompt ↗Push every zero to the back of the array in place while the non-zero values keep their original ordering.
Sweeping a floor: you push all the useful items to one side in the order you meet them, and the empty gaps (zeros) end up collected at the far end.
- Input
- nums = [0, 1, 0, 3, 12]
- Output
- [1, 3, 12, 0, 0]
- Why
- The non-zeros 1, 3, 12 keep their order at the front and both zeros are pushed to the tail.
1 <= nums.length <= 10^4-2^31 <= nums[i] <= 2^31 - 1Follow-up: minimize the total number of operations