(升序)有序数组1长度为m+n,含m个数,有序数组2 长度为n,含n个数。要求直接在数组1上完成合并
方法:从两数组尾部开始比较,比较后大的值插入数组1尾部
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.
"""
if m==0:
nums1[:] = nums2[:]
return
if n==0:
return
k = m+n
while(m*n):
count = 0
while(m*n and nums2[n-1]>=nums1[m-1]):
n -= 1
count += 1
if(count):
nums1[k-count:k] = nums2[n:n+count]
nums1[k-count-1] = nums1[m-1]
m -= 1
k = k-count-1
if(n):
nums1[0:n] = nums2[0:n]
return