-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsort_colors.py
75 lines (60 loc) · 1.88 KB
/
sort_colors.py
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
from typing import List
import unittest
class Solution:
def sortColors(self, nums: List[int]) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
left, right = 0, len(nums) - 1
i = 0
while i <= right:
if nums[i] == 0:
nums[left], nums[i] = nums[i], nums[left]
left += 1
i += 1
elif nums[i] == 2:
nums[right], nums[i] = nums[i], nums[right]
right -= 1
else:
i += 1
# Time complexity: O(n)
# Space complexity: O(1)
class TestSolution(unittest.TestCase):
def setUp(self):
self.solution = Solution()
def test_sortColors(self):
nums = [2,0,2,1,1,0]
self.solution.sortColors(nums)
self.assertEqual(nums, [0,0,1,1,2,2])
nums = [2,0,1]
self.solution.sortColors(nums)
self.assertEqual(nums, [0,1,2])
nums = [0]
self.solution.sortColors(nums)
self.assertEqual(nums, [0])
nums = [1]
self.solution.sortColors(nums)
self.assertEqual(nums, [1])
nums = [2]
self.solution.sortColors(nums)
self.assertEqual(nums, [2])
nums = [1,2,0]
self.solution.sortColors(nums)
self.assertEqual(nums, [0,1,2])
nums = [1,0,2]
self.solution.sortColors(nums)
self.assertEqual(nums, [0,1,2])
nums = [0,2,1]
self.solution.sortColors(nums)
self.assertEqual(nums, [0,1,2])
nums = [0,1,2]
self.solution.sortColors(nums)
self.assertEqual(nums, [0,1,2])
nums = [2,1,0]
self.solution.sortColors(nums)
self.assertEqual(nums, [0,1,2])
nums = [2,0,1,2,1,0]
self.solution.sortColors(nums)
self.assertEqual(nums, [0,0,1,1,2,2])
if __name__ == '__main__':
unittest.main()