Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def merge(self, nums1, m, nums2, n):
- """
- :type nums1: List[int]
- :type m: int
- :type nums2: List[int]
- :type n: int
- :rtype: None Do not return anything, modify nums1 in-place instead.
- """
- idxM = m - 1 # the end index of nums1
- idxN = n - 1 # the end index of nums2
- destination = m + n - 1 # we will write to nums1 from the end
- while destination >= 0:
- if idxM < 0: # if we only have nums2, copy those
- nums1[destination] = nums2[idxN]
- idxN -= 1
- elif idxN < 0: # else if we only have nums1, copy those
- nums1[destination] = nums1[idxM]
- idxM -= 1
- elif nums2[idxN] > nums1[idxM]: # if num2 greater, write it at destination
- nums1[destination] = nums2[idxN]
- idxN -= 1
- else:
- # else num1 is greater, write it at destination and overwrite the existing location with min
- nums1[destination], nums1[idxM] = nums1[idxM], -maxint-1
- idxM -= 1
- destination -= 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement