Skip to content

Latest commit

 

History

History
28 lines (26 loc) · 689 Bytes

088.md

File metadata and controls

28 lines (26 loc) · 689 Bytes

88. Merge Sorted Array

Solution 1

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: void Do not return anything, modify nums1 in-place instead.
        """
        p = m + n - 1
        m -= 1
        n -= 1
        while m >= 0 and n >= 0:
            if nums1[m] >= nums2[n]:
                nums1[p] = nums1[m]
                m -= 1
            else:
                nums1[p] = nums2[n]
                n -= 1
            p -= 1
        if m < 0:
            nums1[:n + 1] = nums2[:n + 1]