mirror of
https://github.com/20kaushik02/leetcode-gulag.git
synced 2025-12-06 07:34:06 +00:00
28 lines
639 B
Python
28 lines
639 B
Python
def merge(self, nums1: list[int], m: int, nums2: list[int], n: int) -> None:
|
|
"""
|
|
Do not return anything, modify nums1 in-place instead.
|
|
"""
|
|
i = 0
|
|
j = 0
|
|
nums3 = []
|
|
# one at a time
|
|
while i < m and j < n:
|
|
if nums1[i] < nums2[j]:
|
|
nums3.append(nums1[i])
|
|
i += 1
|
|
else:
|
|
nums3.append(nums2[j])
|
|
j += 1
|
|
|
|
# deplete nums1
|
|
while i < m:
|
|
nums3.append(nums1[i])
|
|
i += 1
|
|
# deplete nums2
|
|
while j < n:
|
|
nums3.append(nums2[j])
|
|
j += 1
|
|
# optimize this?
|
|
for idx, val in enumerate(nums3):
|
|
nums1[idx] = val
|