Skip to content

Commit d8aabde

Browse files
python: add problem 75 and unittest
1 parent 16793b7 commit d8aabde

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

‎Python/sln_1_100/solution_71_80.py

+21
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,27 @@ def binary_search(func, l):
8181
return True
8282
return False
8383

84+
def sortColors(self, nums: List[int]) -> None:
85+
"""
86+
75
87+
Do not return anything, modify nums in-place instead.
88+
"""
89+
prefix_idx = 0
90+
suffix_idx = len(nums) - 1
91+
idx = 0
92+
while idx <= suffix_idx:
93+
num = nums[idx]
94+
95+
if num == 0 and idx > prefix_idx:
96+
nums[prefix_idx], nums[idx] = nums[idx], nums[prefix_idx]
97+
prefix_idx += 1
98+
idx -= 1
99+
elif num == 2 and idx < suffix_idx:
100+
nums[suffix_idx], nums[idx] = nums[idx], nums[suffix_idx]
101+
suffix_idx -= 1
102+
idx -= 1
103+
idx += 1
104+
84105
def combine(self, n: int, k: int) -> List[List[int]]:
85106
"""
86107
77

‎Python/sln_1_100/test_solution_71_80.py

+8
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,11 @@ def test_search_martrix(self):
3636
def test_combine(self):
3737
ret = self.sln.combine(4, 2)
3838
self.assertEqual(ret, [[1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4]])
39+
40+
def test_sortColors(self):
41+
arr = [2, 0, 2, 1, 1, 0]
42+
self.sln.sortColors(arr)
43+
assert arr == [0, 0, 1, 1, 2, 2]
44+
arr1 = [2, 0, 1]
45+
self.sln.sortColors(arr1)
46+
assert arr1 == [0, 1, 2]

0 commit comments

Comments
 (0)