难点在于idx2是从最末端开始的
class Solution:
def sortColors(self, nums: List[int]) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
idx0 = 0
idx1 = 0
idx2 = len(nums)-1
while idx1<=idx2:
if nums[idx1]==0:
nums[idx0],nums[idx1] = nums[idx1],nums[idx0]
idx0 += 1
idx1 += 1
elif nums[idx1]==1:
idx1 += 1
else:
nums[idx1],nums[idx2] = nums[idx2],nums[idx1]
idx2 -= 1