This commit is contained in:
parent
78c3727dc4
commit
1c6922e62d
1 changed files with 22 additions and 0 deletions
22
medium/sort_colors.py
Normal file
22
medium/sort_colors.py
Normal file
|
|
@ -0,0 +1,22 @@
|
||||||
|
# https://leetcode.com/problems/sort-colors
|
||||||
|
|
||||||
|
from typing import List
|
||||||
|
|
||||||
|
class Solution:
|
||||||
|
def sortColors(self, nums: List[int]) -> None:
|
||||||
|
def swap(i: int, j: int):
|
||||||
|
nums[i], nums[j] = nums[j], nums[i]
|
||||||
|
i = 0
|
||||||
|
j = 0
|
||||||
|
k = len(nums) - 1
|
||||||
|
while j <= k:
|
||||||
|
if nums[j] < 1:
|
||||||
|
swap(i, j)
|
||||||
|
i += 1
|
||||||
|
j += 1
|
||||||
|
elif nums[j] > 1:
|
||||||
|
swap(j, k)
|
||||||
|
k -= 1
|
||||||
|
else:
|
||||||
|
j += 1
|
||||||
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue