Merge Sorted Array
Problem statement
nums1 has length m+n. Its first m values and all n values of nums2 are sorted nondecreasingly; the remaining positions of nums1 are capacity.
Merge both sorted sequences into nums1 and return it.
Function
mergeSortedArray(nums1: int[], m: int, nums2: int[], n: int) → int[]Examples
Example 1
nums1 = [1,2,3,0,0,0]m = 3nums2 = [2,5,6]n = 3return = [1,2,2,3,5,6]The two sorted prefixes merge in nondecreasing order.
Example 2
nums1 = [0]m = 0nums2 = [1]n = 1return = [1]The first sequence is empty.
Constraints
0 <= m, nand1 <= m+n <= 10^5.nums1.length == m+nandnums2.length == n.