-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path88_merge_sorted_array.py
71 lines (63 loc) · 2.46 KB
/
88_merge_sorted_array.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
64
65
66
67
68
69
70
71
# Solution 1
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.
"""
if n == 0:
pass
elif m == 0:
# Remove extra zeros at the end
# 去除末尾多余的0
for tmp in range(n):
nums1.pop()
for i in range(n):
nums1.append(nums2[i])
else:
for tmp in range(n):
nums1.pop()
i = j = 0
while i != m and j != n:
if nums1[i] >= nums2[j]:
nums1.insert(i, nums2[j])
i += 1
j += 1
# a new element is inserted, the length of nums1 is increased by one, that is, m = m + 1
# 因为插入了新元素,nums1长度要增加一,即m=m+1
m += 1
else:
i += 1
if i == m:
for k in range(j, n):
nums1.append(nums2[k])
# Solution 2, easy to understand, but poor efficiency
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.
"""
if n == 0:
pass
else:
# Remove extra zeros at the end
# 去除末尾多余的0
for tmp in range(n):
nums1.pop()
for i in range(n):
nums1.append(nums2[i])
nums1.sort()
'''
This is my personal record of solving Leetcode Problems.
If you have any questions, please discuss them in [Issues](https://github.com/mengxinayan/leetcode/issues).
Copyright (C) 2020-2022 mengxinayan
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
'''