We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Difficulty: 困难
Related Topics: 数组, 二分查找, 分治
给定两个大小分别为 m 和 n 的正序(从小到大)数组 nums1 和 nums2。请你找出并返回这两个正序数组的 中位数 。
m
n
nums1
nums2
算法的时间复杂度应该为 O(log (m+n)) 。
O(log (m+n))
示例 1:
输入:nums1 = [1,3], nums2 = [2] 输出:2.00000 解释:合并数组 = [1,2,3] ,中位数 2
示例 2:
输入:nums1 = [1,2], nums2 = [3,4] 输出:2.50000 解释:合并数组 = [1,2,3,4] ,中位数 (2 + 3) / 2 = 2.5
提示:
nums1.length == m
nums2.length == n
0 <= m <= 1000
0 <= n <= 1000
1 <= m + n <= 2000
Language: JavaScript
/** * @param {number[]} nums1 * @param {number[]} nums2 * @return {number} */ var findMedianSortedArrays = function(nums1, nums2) { const newNums = nums1.concat(nums2).sort((a, b) => a - b) const len = newNums.length if (len % 2 === 0) { return (newNums[len/2 - 1] + newNums[len/2]) / 2 } else { return newNums[Math.floor(len/2)] } }
The text was updated successfully, but these errors were encountered:
No branches or pull requests
4. 寻找两个正序数组的中位数
Description
Difficulty: 困难
Related Topics: 数组, 二分查找, 分治
给定两个大小分别为
m
和n
的正序(从小到大)数组nums1
和nums2
。请你找出并返回这两个正序数组的 中位数 。算法的时间复杂度应该为
O(log (m+n))
。示例 1:
示例 2:
提示:
nums1.length == m
nums2.length == n
0 <= m <= 1000
0 <= n <= 1000
1 <= m + n <= 2000
Solution
Language: JavaScript
The text was updated successfully, but these errors were encountered: