-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy path88.py
48 lines (46 loc) · 1.61 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
__________________________________________________________________________________________________
sample 28 ms submission
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.
"""
i, j = m - 1, n - 1
slot_index = m + n - 1
while i >= 0 and j >= 0:
if nums1[i] > nums2[j]:
nums1[slot_index] = nums1[i]
i -= 1
else:
nums1[slot_index] = nums2[j]
j -= 1
slot_index -= 1
while j >= 0:
nums1[slot_index] = nums2[j]
slot_index -= 1
j -= 1
__________________________________________________________________________________________________
sample 12948 kb submission
class Solution:
def merge(self, nums1: List[int], m: int, nums2: List[int], n: int) -> None:
j, k = 0, 0
index = 0
temp = 0
while index < len(nums1):
if n == 0:
break
if nums1[index] <= nums2[k] and j != m:
j += 1
elif j == m:
nums1[index] = nums2[k]
k+=1
else:
temp = nums2[k]
for i in range((len(nums1)-1), index, -1):
nums1[i] = nums1[i-1]
nums1[index] = nums2[k]
k += 1
if k == n:
break
index += 1
__________________________________________________________________________________________________