Skip to main content

88. Merge Sorted Array

https://leetcode.com/problems/merge-sorted-array/

Difficulty: Easy

Nothing special.

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.
"""
nums1_cp = nums1[:m]
i, j, x = 0, 0, 0

while i < m or j < n:
if i == m:
nums1[x] = nums2[j]
j += 1
elif j == n:
nums1[x] = nums1_cp[i]
i += 1
elif nums1_cp[i] < nums2[j]:
nums1[x] = nums1_cp[i]
i += 1
else:
nums1[x] = nums2[j]
j += 1
x += 1