LeetCode地址
算法难度
中度
JS代码
/**
* @param {number[]} nums1
* @param {number[]} nums2
* @return {number}
*/
var findMedianSortedArrays = function(nums1, nums2) {
let l1 = nums1.length
let l2 = nums2.length
let len = l1 + l2
let left = 0
let right = 0
for (let i = 0; i <= Math.floor(len / 2); i++) {
left = right
if (nums1.length && (!nums2.length || nums1[0] <= nums2[0])) {
right = nums1.shift()
} else {
right = nums2.shift()
}
}
if ((len & 1) === 0) {
return ((right + left) / 2).toFixed(5)
} else {
return right.toFixed(5)
}
};