[Interview classic 150 questions] Merge two ordered arrays - JavaScript version

Topic source

Initial idea : Loop through the two arrays at the same time, select the smaller elements and put them into the new array. The remaining elements of an array that has not been traversed are directly spliced ​​into the new array.

Error example :

var merge = function (nums1, m, nums2, n) {
    
    
    let i = 0,
        j = 0,
        nums3 = [];
    while (i < m && j < n) {
    
    
        if (nums1[i] <= nums2[j]) {
    
    
            nums3.push(nums1[i++]);
        } else {
    
    
            nums3.push(nums2[j++]);
        }
    }
    if (i < m) {
    
    
        nums3 = nums3.concat(nums1.slice(i, m));
    }
    if (j < n) {
    
    
        nums3 = nums3.concat(nums2.slice(j, n));
    }
    nums1 = nums3;
};

nums1 = nums3 The original array will not be changed. This only changes the content of the parameter nums1 pointing to nums3, but the content of the original nums1 has not changed


Correct example 1 :

var merge = function (nums1, m, nums2, n) {
    
    
    let i = 0,
        j = 0,
        nums3 = [];
    while (i < m && j < n) {
    
    
        if (nums1[i] <= nums2[j]) {
    
    
            nums3.push(nums1[i++]);
        } else {
    
    
            nums3.push(nums2[j++]);
        }
    }
    if (i < m) {
    
    
        nums3 = nums3.concat(nums1.slice(i, m));
    }
    if (j < n) {
    
    
        nums3 = nums3.concat(nums2.slice(j, n));
    }
    for (let i = 0; i < nums1.length; i++) {
    
    
        nums1[i] = nums3[i];
    }
    //nums1.splice(0, nums1.length, ...nums3);
};

Directly change the value of the area corresponding to nums1.

spliceNote that there will be a certain space loss when using the method .

Time complexity : O ( m + n ) O(m+n)O(m+n)

Space complexity : O ( m + n ) O(m+n)O(m+n)

Correct example 1 can be optimized : the auxiliary array nums3 can not be used, we traverse from the back to the front, and select the larger ones to fill in nums1.


Correct example 2 : leave out nums3the array

The length of the array nums1 is m+n, and there are n vacancies in the back. We first fill in the larger elements into nums1 from the back to the front. Since the number of vacancies is exactly equal to the length of nums2, there is no risk of losing nums1 elements. When nums1 is traversed Or after nums2 has been traversed, fill all the remaining nums2 into nums1, and nums1 may not be traversed. This situation does not affect the result.

var merge = function(nums1, m, nums2, n){
    
    
    let i = m - 1, j = n - 1, k = m + n - 1;
    while(i>=0&&j>=0){
    
    
        if(nums1[i]>nums2[j]){
    
    
            nums1[k--]=nums1[i--];
        }else{
    
    
            nums1[k--]=nums2[j--];
        }
    }
    while(j>=0){
    
    
        nums1[k--]=nums2[j--];
    }
    // nums1.splice(0,j+1,...nums2.slice(0,j+1));
}

Time complexity : O ( m + n ) O(m+n)O(m+n)

Space complexity : O ( 1 ) O(1)O(1)

Do you guys have a better solution?

Guess you like

Origin blog.csdn.net/2201_75288929/article/details/132420167