Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8c29eb1

Browse files
ybian19azl397985856
authored andcommitted
feat: 88.merge-sorted-array add Python3 implementation (azl397985856#73)
* feat: 88.merge-sorted-array add Python3 implementation * feat: 88.merge-sorted-array add Python3 implementation
1 parent 24dc6a6 commit 8c29eb1

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

‎problems/88.merge-sorted-array.md‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,3 +198,27 @@ public:
198198
}
199199
};
200200
```
201+
202+
Python 代码
203+
```python
204+
class Solution:
205+
def merge(self, nums1: List[int], m: int, nums2: List[int], n: int) -> None:
206+
"""
207+
Do not return anything, modify nums1 in-place instead.
208+
"""
209+
# 整体思路相似,只不过没有使用 current 指针记录当前填补位置
210+
while m > 0 and n > 0:
211+
if nums1[m-1] <= nums2[n-1]:
212+
nums1[m+n-1] = nums2[n-1]
213+
n -= 1
214+
else:
215+
nums1[m+n-1] = nums1[m-1]
216+
m -=1
217+
"""
218+
由于没有使用 current,第一步比较结束后有两种情况:
219+
1. 指针 m>0,n=0,此时不需要做任何处理
220+
2. 指针 n>0,m=0,此时需要将 nums2 指针左侧元素全部拷贝到 nums1 的前 n 位
221+
"""
222+
if n > 0:
223+
nums1[:n] = nums2[:n]
224+
```

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /