
Leetcode Problem 88: Merge Sorted Array
You are given two integer arrays nums1 and nums2, each sorted in non-decreasing order, with m and n valid elements respectively. nums1 has extra space at the end (total length m + n) to hold the merged result. Merge nums2 into nums1 in-place so that nums1 becomes a single sorted array.
Constraints:
nums1 has length m + n; nums2 has length n.m and n are both between 0 and 200, and m + n >= 1.function merge(nums1: number[], m: number, nums2: number[], n: number): void {
let i = m - 1
let j = n - 1
let final = m + n - 1
while (j >= 0) {
if (nums1[i] > nums2[j]) {
nums1[final] = nums1[i]
i--
} else {
nums1[final] = nums2[j]
j--
}
final--
}
};
console.log(merge([1,2,3,0,0,0], 3, [2,5,6], 3))