当前位置:网站首页>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));
}
}
};
边栏推荐
- PCL 最小中值平方法拟合平面
- Unity使用UGUI设置一个简单多级水平方向下拉菜单(不需要代码)
- Practice of traffic recording and playback in vivo
- mysql min() 求某条件下最小的值出现多个结果
- Various entanglements between qvariant and Jason -- QT
- 2022最新最详细必成功的在Vscode中设置背景图、同时解决不受支持的问题
- 原神2.6服务端下载以及搭建安装教程
- July 1st gift: Yi Jingjie's "hundred day battle" ended perfectly, and the database of Guiyang bank was sealed in advance
- PCL 点云镜像变换
- Multi task prompt learning: how to train a large language model?
猜你喜欢

电脑设备打印机驱动安装失败如何解决

Trigger: MySQL implements adding or deleting a piece of data in one table and adding another table at the same time

The login box of unity hub becomes too narrow to log in
![[fluent] dart data type string type (string definition | string splicing | string API call)](/img/7b/cc624aa33f45fbed0bbe354253158b.jpg)
[fluent] dart data type string type (string definition | string splicing | string API call)
![[Yu Yue education] reference materials of sensing and intelligent control technology of Nanjing University of Technology](/img/5c/5f835c286548907f3f09ecb66b2068.jpg)
[Yu Yue education] reference materials of sensing and intelligent control technology of Nanjing University of Technology

潘多拉 IOT 开发板学习(RT-Thread)—— 实验2 RGB LED 实验(学习笔记)

sim2real环境配置教程

SQL solves the problem of continuous login deformation holiday filtering

Compress words (kmp/ string hash, double hash)

Route service grid traffic through two-level gateway design
随机推荐
618深度复盘:海尔智家的制胜方法论
Library management system (Shandong Agricultural University Curriculum Design)
Memory alignment of structure
PCL 最小中值平方法拟合平面
Write your own CPU Chapter 11 - learning notes
(practice C language every day) the sum of the nearest three numbers
[Xiaobai chat cloud] suggestions on container transformation of small and medium-sized enterprises
Leetcode --- longest public prefix
JS learning notes - first acquaintance
Conditions and solutions of deadlock
去除router-link中的下划线
Student course selection system (curriculum design of Shandong Agricultural University)
通过两级网关设计来路由服务网格流量
What is the difference between self attention mechanism and fully connected graph convolution network (GCN)?
源码look me
Construction and business practice of Zhongke brain knowledge map platform
Source code look me
In memory of becoming the first dayu200 tripartite demo contributor
Practice of traffic recording and playback in vivo
路由模式:hash和history模式