leetcode 88. 合并两个有序数组

/**
 * @param {number[]} nums1
 * @param {number} m
 * @param {number[]} nums2
 * @param {number} n
 * @return {void} Do not return anything, modify nums1 in-place instead.
 */
var merge = function(nums1, m, nums2, n) {
    let newArr = []
    let l = 0;
    let r = 0;
    while(l<m&&r<n){
        if(nums1[l]<=nums2[r]){
            newArr.push(nums1[l]);
            l++;
        }else{
            newArr.push(nums2[r]);
            r++;
        }
    }
    while(l<m){
        newArr.push(nums1[l]);
        l++;
    }
    while(r<n){
        newArr.push(nums2[r]);
        r++;
    }
    nums1.splice(0,nums1.length,...newArr);
};

 

posted @ 2021-09-24 15:09  KYSpring  阅读(29)  评论(0编辑  收藏  举报