-
Notifications
You must be signed in to change notification settings - Fork 17
/
merge-sorted-array.py
43 lines (31 loc) · 1.11 KB
/
merge-sorted-array.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
import unittest
class Solution:
def merge(self, nums1, m, nums2, n):
"""
Do not return anything, modify nums1 in-place instead.
"""
free_ptr = m + n - 1
ptr_last1 = m - 1
ptr_last2 = n - 1
while free_ptr > ptr_last1 and ptr_last2 >= 0:
if ptr_last1 >=0 and nums1[ptr_last1] > nums2[ptr_last2]:
nums1[free_ptr] = nums1[ptr_last1]
ptr_last1 -= 1
else:
nums1[free_ptr] = nums2[ptr_last2]
ptr_last2 -= 1
free_ptr -= 1
return nums1
class TestSolution(unittest.TestCase):
def setUp(self):
self.sol = Solution()
def test_empty(self):
self.assertListEqual(self.sol.merge([], 0, [], 0), [])
def test_one1(self):
self.assertListEqual(self.sol.merge([2,0], 1, [1], 1), [1,2])
def test_one2(self):
self.assertListEqual(self.sol.merge([1,0], 1, [2], 1), [1,2])
def test_custom1(self):
self.assertListEqual(self.sol.merge([1,2,3,0,0,0], 3, [2,5,6], 3), [1,2,2,3,5,6])
if __name__ == "__main__":
unittest.main()