merge_ordered_arrays.py 622 B

1234567891011121314151617181920212223242526
  1. from typing import List
  2. def merge(nums1: List[int], m: int, nums2: List[int], n: int) -> None:
  3. # 法1
  4. nums1[m:] = nums2
  5. nums1.sort()
  6. #双指针
  7. Sort = []
  8. p1, p2 = 0, 0
  9. while p1 < m or p2 < n:
  10. if p1 == m:
  11. Sort.append(nums2[p2])
  12. p2 += 1
  13. elif p2 == n:
  14. Sort.append(nums1[p1])
  15. p1 += 1
  16. elif nums1[p1] <= nums2[p2]:
  17. Sort.append(nums1[p1])
  18. p1 += 1
  19. else:
  20. Sort.append(nums2[p2])
  21. p2 += 1
  22. nums1[:] = Sort
  23. print(merge(nums1 = [1,2,3,0,0,0], m = 3, nums2 = [2,5,6], n = 3))