Skip to content

Commit

Permalink
feat: 88.merge-sorted-array add Python3 implementation (azl397985856#73
Browse files Browse the repository at this point in the history
)

* feat: 88.merge-sorted-array add Python3 implementation

* feat: 88.merge-sorted-array add Python3 implementation
  • Loading branch information
ybian19 authored and azl397985856 committed Aug 2, 2019
1 parent 24dc6a6 commit 8c29eb1
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions problems/88.merge-sorted-array.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]
```

0 comments on commit 8c29eb1

Please sign in to comment.