forked from vJechsmayr/PythonAlgorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0075_Sort_Colors.py
More file actions
29 lines (26 loc) · 803 Bytes
/
Copy path0075_Sort_Colors.py
File metadata and controls
29 lines (26 loc) · 803 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# Problem name : Sort Colors
# Problem link : https://leetcode.com/problems/sort-colors/
# Contributor : Shreeraksha R Aithal
class Solution:
def sortColors(self, nums: List[int]) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
i = k = 0
n = len(nums)
j = n-1
while k < n and i < j:
if nums[k] == 0:
if k > i:
nums[i], nums[k] = nums[k], nums[i]
i += 1
else:
k += 1
elif nums[k] == 2:
if k < j:
nums[j], nums[k] = nums[k], nums[j]
j -= 1
else:
k += 1
else:
k += 1