题目
思路
三个指针i,j,k。i指向0,k指向2,j不断遍历,进行swap操作
代码
class Solution:
"""
@param nums: A list of integer which is 0, 1 or 2
@return: nothing
"""
def sortColors(self, nums):
# write your code here
i = 0; j = 0; k = len(nums) - 1
while j <= k:
if nums[j] == 0:
nums[i], nums[j] = nums[j], nums[i]
i += 1;
if i > j: j += 1
elif nums[j] == 2:
nums[j], nums[k] = nums[k], nums[j]
k -= 1;
else: j += 1