- categories: Code, Interview Question, leetcode, Easy
- source: https://leetcode.com/problems/merge-sorted-array/description
- topics: Array, Two Pointers
class Solution(object):
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.
"""
t = m + n - 1
i = m - 1
j = n - 1
while i >= 0 and j >= 0:
if nums1[i] > nums2[j]:
nums1[t] = nums1[i]
i-=1
else:
nums1[t] = nums2[j]
j-=1
t-=1
# Copy remaining data from nums2, for nums1 already in place
while j >= 0:
nums1[t] = nums2[j]
j-=1
t-=1