-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path88_merge_sorted_array.py
More file actions
54 lines (49 loc) · 1.59 KB
/
88_merge_sorted_array.py
File metadata and controls
54 lines (49 loc) · 1.59 KB
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
class Solution(object):
def merge(self, nums1, m, nums2, n):
"""
:type nums1: List[int]
:type m: int
:type nums2: List[int]
:type n: int
:rtype: None Do not return anything, modify nums1 in-place instead.
"""
try:
nums2[0] #Checks for an empty nums2.
keepGoing = True
indexOfComparison = m-1
indexOfInsertion = n+m-1
while keepGoing:
num2 = nums2.pop()
num1 = nums1[indexOfComparison]
if num2 >= num1:
nums1[indexOfInsertion] = num2
else:
nums2.append(num2)
nums1[indexOfInsertion] = num1
indexOfComparison -= 1
#Note that indexOfInsertion >= 0
indexOfInsertion -= 1
#If nums2 is exhausted, stop.
try:
nums2[0]
except:
keepGoing = False
#If nums1 is exhausted, stop.
if indexOfComparison < 0:
keepGoing = False
try:
nums2[0]
keepGoing = True
except:
pass
while keepGoing:
num2 = nums2.pop()
nums1[indexOfInsertion] = num2
indexOfInsertion -= 1
try:
nums2[0]
except:
keepGoing = False
except:
#If nums2 is empty, do nothing.
pass