孙晓聪
  • 最新
  • 博客
  • 书评
  • Merge Two Sorted Array

    Description

    ref

    Solutions

    Recursive Version

    /**
     * @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 idx = m + n - 1
      let idx1 = m - 1
      let idx2 = n - 1
    
      while (idx1 >= 0 && idx2 >= 0) {
        nums1[idx--] = nums1[idx1] > nums2[idx2] ? nums1[idx1--] : nums2[idx2--]
      }
    
      while (idx2 >= 0) {
        nums1[idx--] = nums2[idx2--]
      }
    }
    
    • Time Complexity: O(m + n)
    • Space Complexity: O(m + n)