Python Solution


  • 0
    W
    class Solution:
        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.
            """
            i, j = 0, 0
            while j < n:
                if m > 0:
                    if nums1[i] > nums2[j]:
                        nums1[i+1:i+1+m] = nums1[i:i+m]
                        nums1[i] = nums2[j]
                        j += 1
                    else:
                        m -= 1
                    i += 1
                else:
                    nums1[i] = nums2[j]
                    i += 1
                    j += 1
            return
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.