-
解题思路:快排中的一部分,等于1的放中间,等于0的放左边,等于2的放右边
-
代码
class Solution: def sortColors(self, nums: List[int]) -> None: left_border = -1 right_border = len(nums) i = 0 while i < right_border: if nums[i] == 1: i += 1 elif nums[i] == 0: left_border += 1 nums[i] = nums[left_border] nums[left_border] = 0 i += 1 else: right_border -= 1 nums[i] = nums[right_border] nums[right_border] = 2