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 3386c66

Browse files
Create mergeSortedArray.py
1 parent 1acf17f commit 3386c66

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution(object):
2+
def merge(self, nums1, m, nums2, n):
3+
aIndex, bIndex, mergeIndex = m-1, n-1, m + n - 1
4+
while aIndex >= 0 and bIndex >= 0:
5+
if nums1[aIndex] > nums2[bIndex]:
6+
nums1[mergeIndex] = nums1[aIndex]
7+
aIndex -= 1
8+
else:
9+
nums1[mergeIndex] = nums2[bIndex]
10+
bIndex -= 1
11+
mergeIndex -= 1
12+
while bIndex >= 0:
13+
nums1[mergeIndex] = nums2[bIndex]
14+
bIndex -= 1
15+
mergeIndex -= 1
16+
17+
# learned it here https://www.youtube.com/watch?v=rZ9lcXCWSUg, https://www.byte-by-byte.com/mergearrays/
18+
19+

0 commit comments

Comments
(0)

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