当前位置:网站首页>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));
}
}
};
边栏推荐
- By asp Net core downloads files according to the path exception
- [fluent] dart language (DART language features | JIT instant compilation | AOT static compilation)
- 去除router-link中的下划线
- AWS virtual machine expansion
- 月报总结|Moonbeam6月份大事一览
- SSM整合-异常处理器及项目异常处理方案
- Mobile web development learning notes - Layout
- Leetcode -- number of palindromes
- Student course selection system (curriculum design of Shandong Agricultural University)
- Data security industry series Salon (III) | data security industry standard system construction theme Salon
猜你喜欢

How to use stustr function in Oracle view

Unity使用UGUI设置一个简单多级水平方向下拉菜单(不需要代码)

Yyds dry inventory company stipulates that all interfaces use post requests. Why?

Where can I open computer administrator permissions

Idea public method extraction shortcut key

Original God 2.6 server download and installation tutorial

Set the background picture in the idea (ultra detailed)

Maui学习之路(三)--Winui3深入探讨

路由模式:hash和history模式

mysql 计算经纬度范围内的数据
随机推荐
Kubernetes family container housekeeper pod online Q & A?
请问怎么在oracle视图中使用stustr函数
mysql 计算经纬度范围内的数据
台积电全球员工薪酬中位数约46万,CEO约899万;苹果上调日本的 iPhone 售价 ;Vim 9.0 发布|极客头条...
Comprehensively interpret the background and concept of service mesh
Routing mode: hash and history mode
Invalid bound statement (not found) solution summary
Maui learning road (III) -- in depth discussion of winui3
Foreign enterprise executives, continuous entrepreneurs, yoga and skiing masters, and a program life of continuous iteration and reconstruction
Practice of traffic recording and playback in vivo
Route service grid traffic through two-level gateway design
Figure database | Nepal graph v3.1.0 performance report
Set the background picture in the idea (ultra detailed)
Classifier visual interpretation stylex: Google, MIT, etc. have found the key attributes that affect image classification
JS learning notes - first acquaintance
图书管理系统(山东农业大学课程设计)
Practice of constructing ten billion relationship knowledge map based on Nebula graph
学生选课系统(山东农业大学课程设计)
Summary of multithreading and thread synchronization knowledge
数学分析_笔记_第6章:一元函数的Riemann积分