【medium】4. Median of Two Sorted Arrays 两个有序数组中第k小的数
There are two sorted arrays nums1 and nums2 of size m and n respectively.
Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).
You may assume nums1 and nums2 cannot be both empty.
Example 1:
nums1 = [1, 3]
nums2 = [2] The median is 2.0
Example 2:
nums1 = [1, 2]
nums2 = [3, 4] The median is (2 + 3)/2 = 2.5
class Solution {
public:
/*
对于一个长度为n的已排序数列a,若n为奇数,中位数为第(n/2+1)个数 ,
若n为偶数,则中位数=[第(n/2)个数 + 第(n/2+1)个数] / 2
如果我们可以在两个数列中求出第K小的元素,便可以解决该问题
不妨设数列A元素个数为n,数列B元素个数为m,各自升序排序,求第k小元素
取A[k / 2] B[k / 2] 比较,
如果 A[k / 2] > B[k / 2] 那么,所求的元素必然不在B的前k / 2个元素中(证明反证法)
反之,必然不在A的前k / 2个元素中,于是我们可以将A或B数列的前k / 2元素删去,求剩下两个数列的
k - k / 2小元素,于是得到了数据规模变小的同类问题,递归解决
如果 k / 2 大于某数列个数,所求元素必然不在另一数列的前k / 2个元素中,同上操作就好。
*/
double findKth(vector<int>& A, vector<int>& B, int A_st, int B_st, int k) { //经典函数
// 边界情况,任一数列为空
if (A_st >= A.size()) {
return B[B_st + k - ];
}
if (B_st >= B.size()) {
return A[A_st + k - ];
}
// k等于1时表示取最小值,直接返回min
if (k == ) return min(A[A_st], B[B_st]);
int A_key = A_st + k / - >= A.size() ? INT_MAX : A[A_st + k / - ];
int B_key = B_st + k / - >= B.size() ? INT_MAX : B[B_st + k / - ];
if (A_key < B_key){
return findKth(A, B, A_st + k / , B_st, k - k / );
} else {
return findKth(A, B, A_st, B_st + k / , k - k / );
}
}
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
int sum = nums1.size() + nums2.size();
double ret;
if (sum & ) {
ret = findKth(nums1, nums2, , , sum / + );
} else {
ret = ((findKth(nums1, nums2, , , sum / )) +
findKth(nums1, nums2, , , sum / + )) / 2.0;
}
return ret;
}
};
【medium】4. Median of Two Sorted Arrays 两个有序数组中第k小的数的更多相关文章
- [LeetCode] Median of Two Sorted Arrays 两个有序数组的中位数
There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...
- [LeetCode] 4. Median of Two Sorted Arrays 两个有序数组的中位数
There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...
- 004 Median of Two Sorted Arrays 两个有序数组的中位数
There are two sorted arrays nums1 and nums2 of size m and n respectively.Find the median of the two ...
- [LintCode] Median of Two Sorted Arrays 两个有序数组的中位数
There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted ...
- 【LeetCode】4.Median of Two Sorted Arrays 两个有序数组中位数
题目: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...
- 2.Median of Two Sorted Arrays (两个排序数组的中位数)
要求:Median of Two Sorted Arrays (求两个排序数组的中位数) 分析:1. 两个数组含有的数字总数为偶数或奇数两种情况.2. 有数组可能为空. 解决方法: 1.排序法 时间复 ...
- Leetcode4.Median of Two Sorted Arrays两个排序数组的中位数
给定两个大小为 m 和 n 的有序数组 nums1 和 nums2 . 请找出这两个有序数组的中位数.要求算法的时间复杂度为 O(log (m+n)) . 你可以假设 nums1 和 nums2 不同 ...
- 4. Median of Two Sorted Arrays(2个有序数组的中位数)
There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...
- 4. Median of Two Sorted Arrays *HARD* -- 查找两个排序数组的中位数(寻找两个排序数组中第k大的数)
There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...
随机推荐
- VS2019 实用操作
本文列出了在编写程序过程中的几个非常实用的操作方式,通过这些操作方式,可以在一定程度上减少重复操作.提高编码效率.改善编程体验. 列模式操作 列操作是一项很常用且实用的功能,可以一次性修改不同的行. ...
- 【学习总结】GirlsInAI ML-diary day-19-面向对象编程
[学习总结]GirlsInAI ML-diary 总 原博github链接-day19 认识面向对象 Python使用类(class)和对象(object),进行面向对象(object-oriente ...
- Education CodeForces Round 63 Div.2
A. Reverse a Substring 代码: #include <bits/stdc++.h> using namespace std; int N; string s; int ...
- linux系统docker版本升级或安装
如果存在旧版本,则先卸载 最好先将镜像导出保存,以免升级后丢失或者无法使用 如有正在运行的容器,先停止 $ docker ps -q | xargs docker stop 关闭docker服务 $ ...
- git 版本回滚
1.克隆代码到本地 git clone http://qtools@dev.qtoolsbaby.cn:81/gitlab/qtools/jenkins_ceshi.git 2.git log 查看所 ...
- JS实现 阿拉伯数字金额转换为中文大写金额 可以处理负值
JS实现 阿拉伯数字金额转换为中文大写金额 可以处理负值 //************************* 把数字金额转换成中文大写数字的函数(可处理负值) ****************** ...
- python高级编程笔记一 正则表达式
事例
- 内部yum仓库制作
有些安装收到网络隔离(申请一个到DMZ区的通行证很困难) 使用yum的命令工具,在有网络环境下同步我们的yum仓库,并用http服务器代理和制作repo源进行内部安装. 实操: [root@maste ...
- Windows Server 2012 蓝屏 Wpprecorder.sys 故障
坑爹的园区昨天停电了,导致运行中的服务器中断,来电之后,其中有一台Windows 系统的服务无法运行了,接了个显示器,发现无法进入系统了,挂掉了,这下可完蛋了,虽然做了Radio 磁盘阵列,数据不会丢 ...
- TsinsenA1489 抽奖 【期望】
题目分析: 问题可以转化成将m个球放进n个盒子里,每个盒子的贡献为盒子中球数的平方. 第一问考虑增量. 对于一个原本有$x$个球的盒子,新加一个球的贡献是$2x+1$.期望条件下仍然满足. 第$i$个 ...