寻找两个正序数组的中位数

给定两个大小为 m 和 n 的正序(从小到大)数组 nums1 和 nums2。请你找出这两个正序数组的中位数,并且要求算法的时间复杂度为 O(log(m + n))。你可以假设 nums1 和 nums2 不会同时为空。

function findMedianSortedArrays(nums1, nums2) {
    let arr = nums1.concat(nums2).sort((a,b) => a - b),len = arr.length;
    return len % 2 == 0 ? (arr[len / 2] + arr[(len / 2 - 1)]) / 2 : arr[(len - 1) / 2]
}

Leecode提交通过

注:虽提交通过,但是实现合并两个有序数组的方式,时间复杂度不为 O(log(m + n)),请见合并两个有序数组

posted @ 2020-06-19 12:16  671_MrSix  阅读(283)  评论(0编辑  收藏  举报