yan chuanli 2 lat temu
rodzic
commit
39cd7bf618
2 zmienionych plików z 28 dodań i 0 usunięć
  1. 27 0
      array/merge_ordered_arrays.py
  2. 1 0
      笔记

+ 27 - 0
array/merge_ordered_arrays.py

@@ -0,0 +1,27 @@
+from typing import List
+
+
+def merge(nums1: List[int], m: int, nums2: List[int], n: int) -> None:
+    # 法1
+    nums1[m:] = nums2
+    nums1.sort()
+
+    #双指针
+    Sort = []
+    p1, p2 = 0, 0
+    while p1 < m or p2 < n:
+        if p1 == m:
+            Sort.append(nums2[p2])
+            p2 += 1
+        elif p2 == n:
+            Sort.append(nums1[p1])
+            p1 += 1
+        elif nums1[p1] <= nums2[p2]:
+            Sort.append(nums1[p1])
+            p1 += 1
+        else:
+            Sort.append(nums2[p2])
+            p2 += 1
+    nums1[:] = Sort
+
+print(merge(nums1 = [1,2,3,0,0,0], m = 3, nums2 = [2,5,6], n = 3))

+ 1 - 0
笔记

@@ -0,0 +1 @@
+有排序:用双指针