88.Merge Sorted Array

 

 

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.
        """
        l1, l2, end = m-1, n-1, m+n-1
        while l1 >= 0 and l2 >= 0:
            if nums2[l2] >= nums1[l1]:
                nums1[end] = nums2[l2]
                l2 -= 1
            else:
                nums1[end] = nums1[l1]
                l1 -= 1
            end -= 1
        if l1 < 0:
            while l2 >= 0:
                nums1[end] = nums2[l2]
                l2 -= 1
                end -= 1

 

posted @ 2020-05-09 15:07  星海寻梦233  阅读(108)  评论(0编辑  收藏  举报