问题描述:
python:
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: None Do not return anything, modify nums1 in-place instead."""# Set pointers for nums1 and nums2 respectively.p1 = m - 1 # pointer for nums1p2 = n - 1 # pointer for nums2p = m + n - 1 # pointer for the end of nums1# Merge nums1 and nums2 from the backwhile p1 >= 0 and p2 >= 0:if nums1[p1] > nums2[p2]:nums1[p] = nums1[p1]p1 -= 1else:nums1[p] = nums2[p2]p2 -= 1p -= 1# If there are any remaining elements in nums2, add them to nums1nums1[:p2 + 1] = nums2[:p2 + 1]return nums1