Merge Sorted Array
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 one array sorted in non-decreasing order.The final sorted array must not be returned — it must be stored inside
nums1.Note:
nums1has lengthm + n- the first
melements are the ones to merge - the last
nelements are filled with0and must be ignored
- the first
nums2has lengthn
Example 1
Input: nums1 = [4,5,6,0,0,0], m=3; nums2 = [1,2,3], n=3
Output: [1,2,3,4,5,6]
Explanation: Take the significant elements nums1=[4,5,6] and nums2=[1,2,3]; after a sorted merge we get [1,2,3,4,5,6].
Output: [1,2,3,4,5,6]
Explanation: Take the significant elements nums1=[4,5,6] and nums2=[1,2,3]; after a sorted merge we get [1,2,3,4,5,6].
Example 2
Input: nums1 = [0], m=0; nums2 = [1], n=1
Output: [1]
Explanation: nums1 has no significant elements (m=0), so the result equals nums2 -> [1].
Output: [1]
Explanation: nums1 has no significant elements (m=0), so the result equals nums2 -> [1].
Example 3
Input: nums1 = [1,2,0,0], m=2; nums2 = [2,3], n=2
Output: [1,2,2,3]
Explanation: Take the first m=2 elements of nums1 = [1,2] and merge with nums2=[2,3] into a sorted array -> [1,2,2,3].
Output: [1,2,2,3]
Explanation: Take the first m=2 elements of nums1 = [1,2] and merge with nums2=[2,3] into a sorted array -> [1,2,2,3].
Line 1, Char 1