Skip to content

Commit 69f0401

Browse files
authored
Update sort-colors.py
1 parent 8db3dee commit 69f0401

File tree

1 file changed

+9
-12
lines changed

1 file changed

+9
-12
lines changed

Python/sort-colors.py

+9-12
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,15 @@ def sortColors(self, nums):
88
:rtype: void Do not return anything, modify nums in-place instead.
99
"""
1010
def triPartition(nums, target):
11-
i, j, n = 0, 0, len(nums) - 1
12-
13-
while j <= n:
14-
if nums[j] < target:
15-
nums[i], nums[j] = nums[j], nums[i]
16-
i += 1
17-
j += 1
18-
elif nums[j] > target:
19-
nums[j], nums[n] = nums[n], nums[j]
20-
n -= 1
11+
i, left, right = 0, 0, len(nums)-1
12+
while i <= right:
13+
if nums[i] > target:
14+
nums[i], nums[right] = nums[right], nums[i]
15+
right -= 1
2116
else:
22-
j += 1
17+
if nums[i] < target:
18+
nums[left], nums[i] = nums[i], nums[left]
19+
left += 1
20+
i += 1
2321

2422
triPartition(nums, 1)
25-

0 commit comments

Comments
 (0)