Problem
Solution
# 从后向前填充
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.
"""
current_fill = m + n -1 # 当期填充位置
nums1_i = m - 1 # 当前nums1的指向元素位置
nums2_i = n - 1 # 当前nums2的指向元素位置
while nums1_i >= 0 and nums2_i >= 0: #指针大于等于0的含义为只要指针还有元素可指
if nums1[nums1_i] > nums2[nums2_i]:
nums1[current_fill] = nums1[nums1_i]
nums1_i -= 1
else:
nums1[current_fill] = nums2[nums2_i]
nums2_i -= 1
current_fill -= 1
if nums2_i >= 0:
nums1[0:nums2_i+1] = nums2[0:nums2_i+1]