leetcode88 python merge two sorted arrays

Given two ordered integer arrays  nums1  and  nums2 , merge  nums2  into  nums1  , making num1  an  ordered array.

illustrate:

  • Initialize  nums1  and  nums2 with m  and  n  elements, respectively  .
  • You can assume that  nums1  has enough space (space size greater than or equal to  m + n ) to hold  the elements in nums2  .

Example:

enter:
nums1 = [1,2,3,0,0,0], m = 3
nums2 = [2,5,6],       n = 3

Output:  [1,2,2,3,5,6]
python code
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.
        """
        #Merge first, then reorder, this question does not need to be returned
        nums1[m:m+n]=nums2[:n]
        nums1.sort ()
        



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325687856&siteId=291194637