-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_0088_Merge_Sorted_Array.py
More file actions
31 lines (30 loc) · 947 Bytes
/
Copy path_0088_Merge_Sorted_Array.py
File metadata and controls
31 lines (30 loc) · 947 Bytes
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
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: void Do not return anything, modify nums1 in-place instead.
"""
# Approach 2
i, j = n, 0
nums1[n:m + n] = nums1[0:m]
while i < m + n and j < n:
if nums1[i] < nums2[j]:
nums1[i + j - n] = nums1[i]
i += 1
else:
nums1[i + j - n] = nums2[j]
j += 1
if j < n: nums1[m + j:m + n] = nums2[j:n]
# Approach 1
# i, j = m - 1, n - 1
# while i > -1 and j > -1:
# if nums1[i] > nums2[j]:
# nums1[i+j+1] = nums1[i]
# i -= 1
# else:
# nums1[i+j+1] = nums2[j]
# j -= 1
# if n > -1: nums1[0:j+1] = nums2[0:j+1]