【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 ...
随机推荐
- 手动安装Package Control
手动下载一个package control的包:https://github.com/wbond/package_control 然后Download ZIP后,解压,将解压后的文件夹重命名为 Pac ...
- 时间插件datepicker(jQuery-UI,bootstrap)和jquery-steps的冲突解决。。。
日期插件初始化: $('.prelease_time').flatpickr(); let contentSteps = $("#content_form").steps({ h ...
- jQuery的deferred对象实战应用(附:Exchar动态多条数据展示并在topic展示详细数据)
解决三个后台请求都成功后先比较数据再处理数据的需求 今天碰到了一个问题,我需要创建一个图表,但是需要请求三个接口才能比较出指标数据,于是就看到了deferred对象 理论的补充在这里:http://w ...
- BigDecimal(大浮点数)
因为这个单词,和他的四则运算方法总是记不住,所以写入博客,在没有印象的时候再看看自己的博客. BigDecimal的加减乘除不和double float 一样,他需要使用方法来进行加减乘除. 加法:a ...
- hdu-4635(tarjan缩点)
题意:先给你一个n个点,m条边的有向图,问你最多能够增加多少条边,使得这个图不是一个强连通图 解题思路:考虑最多要添加的边数,所以如果能把初始图划分成两个部分,每个部分都是完全图,这两个部分分别用单向 ...
- Java 8 特性 —— 默认方法和静态方法
Java 8 新增了接口的默认方法.简单说,默认方法就是接口可以有实现方法,而且不需要实现类去实现其方法.我们只需在方法名前面加个 default 关键字即可实现默认方法. 为什么要有这个特性?之前的 ...
- BZOJ5261 Rhyme--广义SAM+拓扑排序
原题链接,不是权限题 题目大意 有\(n\)个模板串,让你构造一个尽量长的串,使得这个串中任意一个长度为\(k\)的子串都是至少一个模板串的子串 题解 可以先看一下这道题 [POI2000]病毒 虽然 ...
- Django中related_name作用
相当于我们使用related代替了在通过一个对象查询出多个对象集合时,使用表名_set来获取 我先定义两个模型,一个是作者,一个是作者出版的书籍,算是一对多的类型. class Person(mode ...
- Qt(MinGW版)在win7 64位上无法播放视频解决方案
[原因分析] Qt自带的MinGW是32位版本,不支持64位的ffmpeg(解码器). 无法播放视频,问题就出在opencv_ffmpeg2411_64.dll(opencv\bin\)上. [解决方 ...
- Java基础--面向对象编程4(多态)
1.多态的概念 多态是指程序中的同一引用类型,使用不同的实例而执行结果不同的. 同一个类型:一般指父类 不同的实例:不同的子类实例 执行结果不同:针对同一方法执行的结果不同 package cn.sx ...