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)).

t int MAX = 0x7fffffff, MIN = 0x80000000;
int kth(vector<int>& nums1, vector<int>& nums2, int sz1, int sz2, int k)
{
#define N1(i) (i<1?MIN:(i>sz1?MAX:nums1[i-1]))
#define N2(i) (i<1?MIN:(i>sz2?MAX:nums2[i-1]))
int l, r, x;
l = ;
r = sz1;
while (l <= r)
{
x = (l + r) >> ;
if (N1(x) > N2(k - x + ))
r = x - ;
else if (N1(x + ) < N2(k - x))
l = x + ;
else
return max(N1(x), N2(k - x));
}
return ; //should not get here
#undef N1
#undef N2
} double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
int sz1 = nums1.size(), sz2 = nums2.size(), sz = sz1 + sz2;
if (sz1 > sz2)
return findMedianSortedArrays(nums2, nums1);
if (sz & )
return kth(nums1, nums2, sz1, sz2, (sz >> ) + );
return (kth(nums1, nums2, sz1, sz2, sz >> ) + kth(nums1, nums2, sz1, sz2, (sz >> ) + )) / 2.0;
}

4. Median of Two Sorted Arrays *HARD* -- 查找两个排序数组的中位数(寻找两个排序数组中第k大的数)的更多相关文章

  1. 查找数组中第k大的数

    问题:  查找出一给定数组中第k大的数.例如[3,2,7,1,8,9,6,5,4],第1大的数是9,第2大的数是8-- 思考:1. 直接从大到小排序,排好序后,第k大的数就是arr[k-1]. 2. ...

  2. Leetcode 4 Median of Two Sorted Arrays 二分查找(二分答案+二分下标)

    貌似是去年阿里巴巴c++的笔试题,没有什么创新直接照搬的... 题意就是找出两个排序数组的中间数,其实就是找出两个排序数组的第k个数. 二分答案,先二分出一个数,再用二分算出这个数在两个排序数组排序第 ...

  3. [LeetCode]Median of Two Sorted Arrays 二分查找两个有序数组的第k数(中位数)

    二分.情况讨论 因为数组有序,所以能够考虑用二分.通过二分剔除掉肯定不是第k位数的区间.如果数组A和B当前处理的下标各自是mid1和mid2.则 1.假设A[mid1]<B[mid2], ①.若 ...

  4. 查找无序数组中第K大的数

    思路: 利用快速排序的划分思想 可以找出前k大数,然后不断划分 直到找到第K大元素 代码: #include <iostream> #include <algorithm> # ...

  5. 【算法之美】求解两个有序数组的中位数 — leetcode 4. Median of Two Sorted Arrays

    一道非常经典的题目,Median of Two Sorted Arrays.(PS:leetcode 我已经做了 190 道,欢迎围观全部题解 https://github.com/hanzichi/ ...

  6. 2.Median of Two Sorted Arrays (两个排序数组的中位数)

    要求:Median of Two Sorted Arrays (求两个排序数组的中位数) 分析:1. 两个数组含有的数字总数为偶数或奇数两种情况.2. 有数组可能为空. 解决方法: 1.排序法 时间复 ...

  7. leetcode第四题:Median of Two Sorted Arrays (java)

    Median of Two Sorted Arrays There are two sorted arrays A and B of size m and n respectively. Find t ...

  8. LeetCode(3) || Median of Two Sorted Arrays

    LeetCode(3) || Median of Two Sorted Arrays 题记 之前做了3题,感觉难度一般,没想到突然来了这道比较难的,星期六花了一天的时间才做完,可见以前基础太差了. 题 ...

  9. 【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 tw ...

随机推荐

  1. 20145335郝昊《网络对抗技术》Exp6 信息搜集技术

    20145335郝昊<网络对抗技术>Exp6 信息搜集技术 实验内容 本次实验的目标是掌握信息搜集的最基础技能.具体有(1)各种搜索技巧的应用(2)DNS IP注册信息的查询 (3)基本的 ...

  2. 【前端】Vue.js实现网格列表布局转换

    网格列表布局转换 实现效果: 实现代码及注释: <!DOCTYPE html> <html> <head> <title>布局转换</title& ...

  3. 质量管理三个概念:QC、QA和QM,你能分得清吗?

    今天这里谈的QC.QA和QM,不是岗位或职位,而是一种概念或质量管理的不同时期所关注的重点. 1.产品 早期的质量管理(工业化雏形期)侧重于对终产品的检测.测试,即QC(品质控制)时代. 这个时期的质 ...

  4. TeeChart的网络资料

    TeeChart坐标轴常见问题 http://www.shaoqun.com/a/54063.aspx TeeChart常用编程语句汇总(C#) http://www.ev get.com/artic ...

  5. 【安装】Mysql在Linux上安装

    1.下载 下载地址:http://dev.mysql.com/downloads/mysql/5.6.html#downloads 下载版本:mysql-5.6.37-linux-glibc2.12- ...

  6. 【第二十八章】 springboot + zipkin(brave定制-AsyncHttpClient)

    brave本身没有对AsyncHttpClient提供类似于brave-okhttp的ClientRequestInterceptor和ClientResponseInterceptor,所以需要我们 ...

  7. ACM-ICPC 2018 南京赛区网络预赛 L. Magical Girl Haze 最短路+分层图

    类似题解 There are NN cities in the country, and MM directional roads from uu to v(1\le u, v\le n)v(1≤u, ...

  8. python filter函数

    number_list = range(-, ) less_than_zero = list(filter(lambda x: x < , number_list)) print(less_th ...

  9. shell 逻辑操作符

    Shell还提供了与( -a ).或( -o ).非( ! )三个逻辑操作符用于将测试条件连接起来,其优先级为:"!"最高,"-a"次之,"-o&qu ...

  10. SQL 常用的命令

    --修改表名 --格式:SP_RENAME TABLENAME,NEWTABLENAME SP_RENAME TABLENAME,NEWTABLENAME --只能对表,不能对临时表 --修改字段名 ...