```
class Solution(object):
def swap(self, nums, i, j):
t = nums[i]
nums[i] = nums[j]
nums[j] = t
def sortColors(self, nums):
n = len(nums); l = 0; r = n - 1; i = 0
while l < r and i < r+1:
while nums[i] == 2 and i<r+1:
self.swap(nums, i, r)
r -= 1
if nums[i] == 0:
self.swap(nums, i, l)
l += 1
i += 1
```