LeetCode/Top150Interview/88MergeSortedArray.py
2023-06-26 16:39:03 -07:00

23 lines
705 B
Python

def merge(self, nums1, m, nums2, n):
"""
:type nums1: List[int]
:type m: int
:type nums2: List[int]
:type n: int
:rtype: None Do not return anything, modify nums1 in-place instead.
"""
count = 0
# merge unsorted arrays
for j in range(len(nums1)):
if nums1[j] == 0:
nums1[j] = nums2[count]
count += 1
# # array is filled, now sort, used bubble sort
for i in range(len(nums1)):
for j in range(len(nums1) - i - 1):
if nums1[j] > nums1[j + 1]:
nums1[j], nums1[j+1] = nums1[j+1], nums1[j]
print(nums1)
# merge(0, [1,2,3,0,0,0], 3, [2,5,6], 3)
merge(0, [4,5,6,0,0,0], 3, [1,2,3], 3)