乐趣区

关于javascript:LeetCode-题解|4-寻找两个正序数组的中位数-Javascript

function findMedianSortedArrays(nums1, nums2) {const nums = [].concat(nums1, nums2).sort((prev, next) => prev - next);
  const len = nums.length;
  if (len % 2 === 0) {
    const index = len / 2;
    return (nums[index - 1] + nums[index]) / 2;
  } else {return nums[Math.floor(len / 2)];
  }
}

console.log(findMedianSortedArrays([1, 3], [2]));
console.log(findMedianSortedArrays([1, 2], [3, 4]));
console.log(findMedianSortedArrays([3], [-2, -1]));

本文由一文多发经营工具平台 EaseWriting 公布

退出移动版