看似简单,实际上想了挺久的
1)内置api方法
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. """ while nums1 and nums1[-1] == 0: if len(nums1) == m: break del nums1[-1] nums1 += nums2 nums1.sort()
2)双指针,哪个小输入哪个,再替换Nums1
temp = [] p1 = 0 p2 = 0 while(p1 < m and p2 < n): if (nums1[p1] <= nums2[p2]): temp.append(nums1[p1]) p1 += 1 else: temp.append(nums2[p2]) p2 += 1 while(p1 < m): temp.append(nums1[p1]) p1 += 1 while(p2 < n): temp.append(nums2[p2]) p2 += 1 for i in range(0, m + n ): nums1[i] = temp[i]
3)从后往前