Given two positive order (from small to large) arrays nums1 and nums2 with sizes m and n respectively. Please find and return the median of these two ordinal arrays.
The time complexity of the algorithm should be O(log (m+n)).
Example 1:
Input: nums1 = [1,3], nums2 = [2]
Output: 2.00000
Explanation: merged array = [1,2,3], median 2
Example 2:
Input: nums1 = [1,2], nums2 = [3,4]
Output: 2.50000
Explanation: merged array = [1,2,3,4] , median (2 + 3) / 2 = 2.5
hint:
nums1.length == m
nums2.length == n
0 <= m <= 1000 0 <= n <= 1000
1 <= m + n <= 2000
-106 <= nums1[i], nums2[i] <= 106(power)
answer:
/**
* @param {number[]} nums1
* @param {number[]} nums2
* @return {number}
*/
var findMedianSortedArrays = function(nums1, nums2) {
const num = nums1.concat(nums2).sort((a,b)=>a-b)
if(num.length % 2 === 0){
return (num[num.length/2] + num[num.length/2 - 1])/2
}else
return num[Math.floor(num.length/2)]
};