Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- Bucket sort: TC is O(n).
- There's this case nums[curr]==2, after swapping with right, we should not increase i since the number swapped back from p2 can be 0/1, which needs to be further processed
- When curr pointer has passed left pointer, the number swapped from left can only be
- 1 as curr pointer has passeed left.
- Both of them are tricky to understadn
- """
- class Solution:
- def sortColors(self, nums: List[int]) -> None:
- """
- Do not return anything, modify nums in-place instead.
- """
- left = 0
- right = len(nums)-1
- i = 0
- while i <= right:
- if nums[i] == 0:
- nums[i], nums[left] = nums[left], nums[i]
- left += 1
- elif nums[i] == 2:
- nums[i], nums[right] = nums[right], nums[i]
- right -= 1
- i -= 1
- i += 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement