当前位置:网站首页>LeetCode 4. 寻找两个正序数组的中位数(hard)
LeetCode 4. 寻找两个正序数组的中位数(hard)
2022-07-02 13:15:00 【_刘小雨】
给定两个大小分别为 m 和 n 的正序(从小到大)数组 nums1 和 nums2。请你找出并返回这两个正序数组的 中位数 。
算法的时间复杂度应该为 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
class Solution {
public:
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
//使用递归处理
int total = nums1.size() + nums2.size();
if(total % 2 == 0)
{
int left = findKnum(nums1, 0, nums2, 0, total/2);
int right = findKnum(nums1, 0, nums2,0, total/2 + 1);
return (left + right) / 2.0;
}
else
return findKnum(nums1, 0, nums2, 0, total/2 + 1);
}
int findKnum(vector<int>& nums1, int i, vector<int>& nums2, int j, int k)
{
// nums1 比较长, 短的放在前面
if(nums1.size() - i > nums2.size() - j)
return findKnum(nums2, j, nums1, i, k);
// 左边数组是空的
if(nums1.size() == i) return nums2[j + k - 1];
// k == 1 直接返回两个数组的最小值
if(k == 1) return min(nums1[i], nums2[j]);
int si = min(i + k /2, int(nums1.size())), sj = j + k /2;
if(nums1[si - 1] > nums2[sj - 1])
{
return findKnum(nums1, i, nums2, j+ k /2, k - k /2);
}
else
{
return findKnum(nums1, si, nums2,j, k - (si - i));
}
}
};
边栏推荐
- 触发器:Mysql实现一张表添加或删除一条数据,另一张表同时添加
- day4
- 微信v3native支付设置的结束时间处理办法
- Everyone Xinfu builds: a one-stop intelligent business credit service platform
- 图书管理系统(山东农业大学课程设计)
- Written by unity Jason
- Seal Library - installation and introduction
- Mathematical analysis_ Notes_ Chapter 5: univariate differential calculus
- What is Amazon keyword index? The consequences of not indexing are serious
- SSM integration exception handler and project exception handling scheme
猜你喜欢
Bib | graph representation based on heterogeneous information network learning to predict drug disease association
Headline | Asian control technology products are selected in the textile and clothing industry digital transformation solution key promotion directory of Textile Federation
Vscode设置标签页多行显示
[Yu Yue education] reference materials of sensing and intelligent control technology of Nanjing University of Technology
历史上的今天:支付宝推出条码支付;分时系统之父诞生;世界上第一支电视广告...
Yyds dry goods inventory hands-on teaching you to carry out the packaging and release of mofish Library (fishing Library)
dried food! Understand the structural vulnerability of graph convolution networks
电脑设备打印机驱动安装失败如何解决
微信v3native支付设置的结束时间处理办法
Yyds dry inventory company stipulates that all interfaces use post requests. Why?
随机推荐
Bone conduction non ear Bluetooth headset brand, bone conduction Bluetooth headset brand recommendation
MySQL calculates the data within the longitude and latitude range
By asp Net core downloads files according to the path exception
JS learning notes - variables
Compress words (kmp/ string hash, double hash)
OSPF - detailed explanation of NSSA area and full NSSA area (including configuration command), LSA type 7 lsa-7
总结|机器视觉中三大坐标系及其相互关系
How to use stustr function in Oracle view
Storage, reading and writing of blood relationship data of Nepal Graph & Data Warehouse
Routing mode: hash and history mode
Boot transaction usage
sql解决连续登录问题变形-节假日过滤
IDEA中设置背景图片(超详细)
Best practices for building multi architecture images
Memory alignment of structure
理想之光不灭
Maui learning road (III) -- in depth discussion of winui3
Yyds dry inventory method of deleting expired documents in batch
Where can I open computer administrator permissions
618 deep resumption: Haier Zhijia's winning methodology