A list contains only the values 0, 1 and 2 (think of three paint colours). Return it sorted in increasing order, without using a general-purpose sort.
A fast solution reads the list once. The tests can only check the answer, not how you got it, so the walkthrough after you solve it shows the one-pass idea. Aim for it anyway: it is a classic.
Example 1
Input:
sort_colors([2, 0, 2, 1, 1, 0])
Output:
[0, 0, 1, 1, 2, 2]
Example 2
Input:
sort_colors([1, 0])
Output:
[0, 1]
0 <= len(nums) <= 3 * 10**5nums[i] is 0, 1 or 2.No results yet
Run your code to check it against the examples, or submit to run every test.