sort_colors.py

mail@pastecode.io avatar
unknown
python
a month ago
548 B
13
Indexable
Never
class Solution:
    def sortColors(self, nums: List[int]) -> None:
        low, mid = 0, 0
        high = len(nums) - 1


        while mid <= high:
            if nums[mid] == 0:
            # argument unpack
                nums[low], nums[mid] = nums[mid], nums[low]
                low += 1
                mid += 1
            elif nums[mid] == 1:
                mid += 1
            # argument unpack
            else:  # nums[mid] == 2
                nums[mid], nums[high] = nums[high], nums[mid]
                high -= 1
Leave a Comment