Merge Two Sorted Arrays
Given sorted integer arrays nums1 and nums2 and valid counts m and n, merge them into the original nums1 in non-decreasing order. nums1 has length m+n: its…
Given sorted integer arrays nums1 and nums2 and valid counts m and n, merge them into the original nums1 in non-decreasing order. nums1 has length m+n: its first m entries are valid values and its last n entries are zero placeholders. nums2 has length n. Return no value. Duplicates are retained; changing nums2 is permitted.
- 0 <= m,n <= 1000 - nums1.length == m+n; nums2.length == n - -10000 <= nums1[i],nums2[i] <= 10000 - nums1[0:m] and nums2 are sorted; nums1[m:m+n] contains zero placeholders. - Both valid prefixes may be empty. - The teaching trace accepts two valid prefixes totaling at most 12 values, derives m/n and zero padding; coding retains all four arguments and complete bounds.
Example 1: input nums1 = [-5,-2,4,5,0,0,0], m = 4, nums2 = [-3,1,8], n = 3; output [-5,-3,-2,1,4,5,8]. The three spare zeros are placeholders, not extra input values.
Example 2: input nums1 = [], m = 0, nums2 = [], n = 0; output []. Both valid prefixes are empty, so nums1 remains empty and the function returns no value.
Category: Two Pointers. Topic: two-pointers. Difficulty: Medium. Patterns: two-pointers.