看了大家的题解,找了一个看似最简单的翻译成Python版本,结果发现这个思路并不简单,把while和if前后位置换一下, [1,2,0] 这个用例会运行失败
class Solution: def sortColor(self , colors: List[int]) -> List[int]: # write code here l = 0 r = len(colors)-1 for i in range(len(colors)): while i <= r and colors[i] == 2: temp = colors[r] colors[r] = colors[i] colors[i] = temp r -= 1 if colors[i] == 0: temp = colors[l] colors[l] = colors[i] colors[i] = temp l += 1 return colors