Merge Sorted Array
You are given two sorted integer arrays nums1 and nums2, and integers m and n representing the number of elements in each.
Merge nums2 into nums1 in-place so that nums1 is sorted. nums1 has length m + n with the last n elements set to 0 (placeholders).
The function returns nothing: it only modifies nums1. The tests then print the merged nums1 as space-separated integers, so do not print inside the function.
Example
1,2,3,0,0,0 3 2,5,6 3
1 2 2 3 5 6
- Inputs:
nums1has length , but only its first entries () are real values. The three trailing zeros are empty slots, not data.nums2holds the other values. - Math: take all six values, and , and arrange them in non-decreasing order: . The value is in both inputs, so it appears twice.
- Output:
1 2 2 3 5 6, exactly numbers, held in the samenums1list that was passed in, overwriting the placeholder slots. No zero survives because none of the zeros was a real value. - Notice the edge cases: with every slot of
nums1is a placeholder and the answer is justnums2; withnums1is already the answer.
Constraints
- 0 <= m, n <= 200
- 1 <= m + n <= 200
- -10^9 <= nums1[i], nums2[i] <= 10^9