# leetcode 88. Merge Sorted Array（python）

2022-01-29 20:22:54

This article has participated in 「 Digging force Star Program 」, Win a creative gift bag , Challenge creation incentive fund .

### describe

You are given two integer arrays nums1 and nums2, sorted in non-decreasing order, and two integers m and n, representing the number of elements in nums1 and nums2 respectively.

Merge nums1 and nums2 into a single array sorted in non-decreasing order.

The final sorted array should not be returned by the function, but instead be stored inside the array nums1. To accommodate this, nums1 has a length of m + n, where the first m elements denote the elements that should be merged, and the last n elements are set to 0 and should be ignored. nums2 has a length of n.

Example 1:

``````Input: nums1 = [1,2,3,0,0,0], m = 3, nums2 = [2,5,6], n = 3
Output: [1,2,2,3,5,6]
Explanation: The arrays we are merging are [1,2,3] and [2,5,6].
The result of the merge is [1,2,2,3,5,6] with the underlined elements coming from nums1.
Copy code ``````

Example 2:

``````Input: nums1 = , m = 1, nums2 = [], n = 0
Output: 
Explanation: The arrays we are merging are  and [].
The result of the merge is .
Copy code ``````

Example 3:

``````Input: nums1 = , m = 0, nums2 = , n = 1
Output: 
Explanation: The arrays we are merging are [] and .
The result of the merge is .
Note that because m = 0, there are no elements in nums1. The 0 is only there to ensure the merge result can fit in nums1.
Copy code ``````

Note:

``````nums1.length == m + n
nums2.length == n
0 <= m, n <= 200
1 <= m + n <= 200
-10^9 <= nums1[i], nums2[j] <= 10^9
Copy code ``````

### analysis

According to the meaning , Is to give a list of two ordered integers nums1 and nums2 , And give two integers m and n , They represent nums1 and nums2 The length of , The title requires us to merge these two ordered integer lists , Returns a list of integers in ascending order .

The final sorted list does not open up new memory space , It's stored in a list nums1 in . Because it has been agreed in the title nums1 The length of is m + n, The top m Elements represent the valid elements to be merged , after n The elements are set to 0 Just to occupy a seat , take nums2 integrate into nums1 After sorting, you can just put down .

In fact, the idea of this question is relatively simple ：

• When n by 0 When , Express nums1 You don't need to be with nums2 A merger , No matter nums1 What is it , Go straight back to nums1 That's all right.

• When m by 0 When , Express nums1 It's empty , There are no elements to merge , But you can't directly return to nums2 , Because the title requires that the last thing to return is nums1 , So no matter nums2 How many elements are there , Directly assign to nums1 , And then back again nums1 Can only be

• from nums1 The last valid element of and nums2 The last valid element of starts the inverse comparison of size ：

• If nums1[m-1] Greater than or equal to nums2[n-1] , Will nums1[m-1] Assign a value to nums1[m+n-1] , meanwhile m Minus one
• Otherwise it would be nums2[n-1] Assign a value to nums1[m+n-1] , meanwhile n Minus one
• If nums2 End of traversal first , because nums1 The remaining elements are ordered from the beginning , So leave him alone , But if nums 1 End of traversal first , Probably nums2 There are other elements that have not been traversed , because nums2 The remaining elements are ordered from the beginning , Direct will nums2[:n] Assign a value to nums1[:n] that will do

• return nums1

``````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: None Do not return anything, modify nums1 in-place instead.
"""
if n == 0:
return nums1
if m == 0:
nums1[:n] = nums2[:n]
return nums1
while m>0 and n>0:
if nums1[m-1]>=nums2[n-1]:
nums1[m+n-1] = nums1[m-1]
m -= 1
else:
nums1[m+n-1] = nums2[n-1]
n -= 1
nums1[:n] = nums2[:n]
return nums1

Copy code ``````

### Running results

``````Runtime: 38 ms, faster than 20.56% of Python online submissions for Merge Sorted Array.
Memory Usage: 13.6 MB, less than 16.74% of Python online submissions for Merge Sorted Array.
Copy code ``````