My python3 solution


  • 0
    Y
    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.
            1 3 5 7 9
            2 4 6 8 10
            """
            if not m:
                nums1[:] = nums2[:]
                return
            if not n:
                return
            point_m1 = m - 1
            point_m2 = n - 1
            for index in range(m+n-1, -1, -1):
                if point_m1 == -1:
                    nums1[index] = nums2[point_m2]
                    point_m2 -= 1
                    continue
                if point_m2 == -1:
                    nums1[index] = nums1[point_m1]
                    point_m1 -= 1
                    continue
                if  nums1[point_m1] >= nums2[point_m2]:
                    nums1[index] = nums1[point_m1]
                    point_m1 -= 1
                else:
                    nums1[index] = nums2[point_m2]
                    point_m2 -= 1
    
    

Log in to reply
 

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