美文网首页LeetCode
88. 合并两个有序数组

88. 合并两个有序数组

作者: cptn3m0 | 来源:发表于2019-04-01 22:55 被阅读0次
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.
        """
        
        # O(m+n), space O(1)
        k = (m+n-1)
        i = m-1
        j = n-1
        while i>=0 and j>=0:
            if nums1[i] > nums2[j]:
                nums1[k] = nums1[i]
                i = i-1
            else:
                nums1[k] = nums2[j]
                j = j-1 
            k = k - 1
        while j >= 0 :
            nums1[k] = nums2[j]
            k = k-1
            j = j-1

相关文章

网友评论

    本文标题:88. 合并两个有序数组

    本文链接:https://www.haomeiwen.com/subject/fpribqtx.html