forked from wuduhren/leetcode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmerge-sorted-array.py
More file actions
25 lines (22 loc) · 813 Bytes
/
merge-sorted-array.py
File metadata and controls
25 lines (22 loc) · 813 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
#https://leetcode.com/problems/merge-sorted-array/
class Solution(object):
def merge(self, nums1, m, nums2, n):
#Compare nums1 and nums2 from backward
#Put the larger one to nums1's end
#If one of the array is done
#The rest is already sorted
#Just put them in place
i = m+n-1 #cursor on nums1 that we are editting
i1 = m-1 #cursor on nums1
i2 = n-1 #cursor on nums2
while i1>=0 and i2>=0:
if nums1[i1]>nums2[i2]:
nums1[i] = nums1[i1]
i1 = i1-1
i = i-1
else:
nums1[i] = nums2[i2]
i2 = i2-1
i = i-1
#One of the array is done, put the rest in place
nums1[:i2+1] = nums2[:i2+1]