Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c392978

Browse files
add solution in Python for 0088_merge_sorted_array.py
1 parent e1e0af4 commit c392978

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
def merge(self, nums1: List[int], m: int, nums2: List[int], n: int) -> None:
2+
"""
3+
Do not return anything, modify nums1 in-place instead.
4+
"""
5+
# Iterate through both of the given arrays simultaneously
6+
while (m > 0 and n > 0):
7+
curr1 = nums1[m - 1]
8+
curr2 = nums2[n - 1]
9+
10+
if(curr1 >= curr2):
11+
nums1[(m + n) - 1] = curr1
12+
m -= 1
13+
else:
14+
nums1[(m + n) - 1] = curr2
15+
n -= 1
16+
# If there are smaller elements left in nums2, copy them over
17+
# No need to do this for leftover nums1 elements, since we are
18+
# merging into nums1 and the elements will already be there
19+
while (n > 0):
20+
nums1[n - 1] = nums2[n - 1]
21+
n -= 1
22+
23+
return nums1

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /