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]));
本文由一文多發(fā)運(yùn)營(yíng)工具平臺(tái) EaseWriting 發(fā)布