Sort Colors
nums holds only the numbers 0, 1 and 2 (think of red, white and blue). Rearrange it in place so all 0s come first, then all 1s, then all 2s. Return nothing.
Try to do it without calling sort.
Example 1
nums [2,0,2,1,1,0]
returns [0,0,1,1,2,2]
It becomes [0, 0, 1, 1, 2, 2].
Constraints
1 <= len(nums) <= 30- Each item is 0, 1 or 2.
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Two pointers problems
- Stretches With a Sum in RangeMedium
- Subarrays With Small ProductMedium
- Three SumMedium
- Trim Both Ends to a TotalMedium
- Valid Palindrome IIMedium
- Valid Triangle NumberMedium
- Water Between the WallsMedium
- Best Path Across Two ListsHard