Sort an Array of 0s, 1s and 2s
MediumP1Coding
nums contains only the values 0, 1 and 2. Sort it in place in ascending order, without using a library sort.
Aim for a single pass with constant extra space.
Example 1
- Input
- nums = [2,0,2,1,1,0]
- Output
- [0,0,1,1,2,2]
Example 2
- Input
- nums = [2,0,1]
- Output
- [0,1,2]
Example 3
- Input
- nums = [0]
- Output
- [0]
Constraints
- 1 <= nums.length <= 10^5
- nums[i] is 0, 1 or 2.
Hints
Give it a real attempt first. Run shows which examples fail.
Saved automatically
Run checks the 3 visible tests, Submit all 8. ⌘Enter runs, ⌘⇧Enter submits.