Sort Colors

Medium

nums contains 0s, 1s and 2s representing red, white and blue. Return the array sorted so equal colours are adjacent, in the order red, white, blue.

Example 1

sortColors()
Input
nums = [2,0,2,1,1,0]
Output
[0,0,1,1,2,2]

Example 2

sortColors()
Input
nums = [2,0,1]
Output
[0,1,2]
Constraints
1 <= nums.length <= 300
`nums[i]` is `0`, `1` or `2`.

Topics

ArraysTwo PointersSorting

Implement sortColors in the editor and return the answer — printing it is never graded.