From 8c29eb10e37dcd6a60873ca9ef1627297a75e424 Mon Sep 17 00:00:00 2001 From: guyu Date: Fri, 2 Aug 2019 09:44:09 +0800 Subject: [PATCH] feat: 88.merge-sorted-array add Python3 implementation (#73) * feat: 88.merge-sorted-array add Python3 implementation * feat: 88.merge-sorted-array add Python3 implementation --- problems/88.merge-sorted-array.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/problems/88.merge-sorted-array.md b/problems/88.merge-sorted-array.md index 4fc2e92a5..9078460aa 100644 --- a/problems/88.merge-sorted-array.md +++ b/problems/88.merge-sorted-array.md @@ -198,3 +198,27 @@ public: } }; ``` + +Python 代码 +```python +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 指针记录当前填补位置 + while m > 0 and n > 0: + if nums1[m-1] <= nums2[n-1]: + nums1[m+n-1] = nums2[n-1] + n -= 1 + else: + nums1[m+n-1] = nums1[m-1] + m -=1 + """ + 由于没有使用 current,第一步比较结束后有两种情况: + 1. 指针 m>0,n=0,此时不需要做任何处理 + 2. 指针 n>0,m=0,此时需要将 nums2 指针左侧元素全部拷贝到 nums1 的前 n 位 + """ + if n > 0: + nums1[:n] = nums2[:n] +```