-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmergeSortedArray.py
More file actions
34 lines (28 loc) · 1021 Bytes
/
mergeSortedArray.py
File metadata and controls
34 lines (28 loc) · 1021 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
29
30
31
32
33
34
class Solution:
def merge(self, nums1: list[int], m: int, nums2: list[int], n: int) -> None:
"""
Do not return anything, modify nums1 in-place instead.
"""
insert_index = len(nums1) - 1 # 5
ptr1 = m - 1
ptr2 = n - 1
# if the nums1 array is empty, then simply add all elements from nums2 to nums1
while ptr2 >= 0:
# print("hello")
if ptr1 == -1:
while ptr2 >= 0:
nums1[insert_index] = nums2[ptr2]
insert_index -= 1
ptr2 -= 1
else:
if nums2[ptr2] > nums1[ptr1]:
nums1[insert_index] = nums2[ptr2]
ptr2 -= 1
else:
nums1[insert_index] = nums1[ptr1]
ptr1 -= 1
insert_index -= 1
solution1 = Solution()
nums1 = [1, 2, 3, 0, 0, 0]
nums2 = [2, 5, 6]
solution1.merge(nums1, 3, nums2, 3)