-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMerge Sorted Array.py
46 lines (33 loc) · 1.32 KB
/
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
# Link: https://leetcode.com/problems/merge-sorted-array/submissions/
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 nothing needs to be added to nums1, exit function
if n <= 0:
return
# Create index pointer to last element of nums1
lastIdx = len(nums1) - 1
# Iterate both lists
while m > 0 and n > 0:
# Compare both values
if nums1[m - 1] > nums2[n - 1]:
# Update last element
nums1[lastIdx] = nums1[m - 1]
# Update index pointers
lastIdx -= 1
m -= 1
else:
# Update last element
nums1[lastIdx] = nums2[n - 1]
# Update index pointers
lastIdx -= 1
n -= 1
# Check if nums2 has any leftover elements
while n > 0:
# Update last element
nums1[lastIdx] = nums2[n - 1]
# Update index pointers
lastIdx -= 1
n -= 1