easy
arrays
two-pointers
Reorder nums so every zero appears after all non-zero values while preserving the relative order of non-zero values. Return the reordered array; implementations should update the supplied array when the language supports mutation.
Input / output
- Input:
nums: integer[] - Output: the reordered integer array
Examples
[0,1,0,3,12]returns[1,3,12,0,0].[1,2,3]remains[1,2,3].
Constraints
0 <= nums.length <= 100,000-1,000,000 <= nums[i] <= 1,000,000
Follow-up Can you use two pointers with linear time, constant auxiliary space, and the minimum necessary writes?
Examples
Example 1
Input: nums = [0,1,0,3,12]
Output: [1,3,12,0,0]
Example 2
Input: nums = [0]
Output: [0]
Example 3
Input: nums = [1,2,3]
Output: [1,2,3]
🔒 5 hidden
Running will execute all 8 cases, including 5 hidden ones.