-
Notifications
You must be signed in to change notification settings - Fork 0
/
88.合并两个有序数组.py
63 lines (53 loc) · 1.57 KB
/
88.合并两个有序数组.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#
# @lc app=leetcode.cn id=88 lang=python3
#
# [88] 合并两个有序数组
#
# @lc code=start
from typing import List
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.
"""
cursor = m + n - 1
m -= 1
n -= 1
while n >= 0:
if m >= 0 and nums1[m] > nums2[n]:
nums1[cursor] = nums1[m]
m -= 1
else:
nums1[cursor] = nums2[n]
n -= 1
cursor -= 1
def merge1(self, nums1: List[int], m: int, nums2: List[int], n: int) -> None:
"""
Do not return anything, modify nums1 in-place instead.
双指针控制 nums1 和 nums2 的索引从后往前移动
cursor 控制 nums1 指针从后往前移动, 直到等于 0
通过比较 nums1 和 nums2 的最后面的值的大小, 放入 cursor 的位置
"""
cursor = m + n
while cursor > 0:
cursor -= 1
if m == 0:
nums1[cursor] = nums2[n - 1]
n -= 1
continue
elif n == 0:
break
i, j = nums1[m - 1], nums2[n - 1]
if i > j:
nums1[cursor] = i
m -= 1
else:
nums1[cursor] = j
n -= 1
if __name__ == "__main__":
# 0 1 2 3 4 5
x = [7, 8, 9, 0, 0, 0]
y = [10, 12, 13]
Solution().merge(x, 3, y, 3)
print(x)
# @lc code=end