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. Python3 Selenium自动化测试赋值出现:WebDriverException: Message: unknown error: call function result missing 'value'

    Python3 Selenium自动化测试赋值出现:WebDriverException: Message: unknown error: call function result missing ' ...

  2. 简单的Django向HTML展示动态图片 案例——小白

    目标:通过Django向HTML传送图片展示 我的天哪,真是膈应人,网上的案例都不适合我,感觉所有的解决办法在我这里都不行. 好吧~ 是我菜,看不懂人家的代码,那句话叫啥来着?一本好经被傻和尚念歪了. ...

  3. ”MySQL查询优化“学习总结

    查询优化有几种方法,下面分别介绍. 切分查询 一条大的语句(涉及很多行)一次会锁住很多数据(不利于高并发). 占满整个事务日志,耗尽系统资源.阻塞很多小的但很重要的查询. 分解关联查询 关联查询分解方 ...

  4. csharp编写界面,opencv编写类库,解决 Pinvoke过程中的参数传递和平台调用问题

        使用csharp 编写winform程序,不仅速度快,而且容易界面美化并找到其他类库的支持:而使用 opencv编写图形图像处理程序,是目前比较流行,而且高效的一种方法.如果需要将两者结合,需 ...

  5. 《课程设计》——foremost的使用

    <课程设计>--foremost的使用 foremost简介 formost 是一个基于文件头和尾部信息以及文件的内建数据结构恢复文件的命令行工具.这个过程通常叫做数据挖掘(data ca ...

  6. 七个月学习Python大计

    仅以此篇纪念学习Python征程的开始

  7. noip2015 day1

    不解释,很简单,直接按照题目的方法构造就行了 Code #include<iostream> #include<cstdio> #include<cctype> # ...

  8. VC 线程池

    参照:http://www.cnblogs.com/kzloser/archive/2013/03/11/2909221.html 参照:http://blog.csdn.net/pjchen/art ...

  9. Android 自动化测试介绍

    1 介绍: 风格: 3, 4,

  10. Delphi XE5 for Android (五)

    Android程序开发必然用到按钮,在XE5下,按钮的一个比较重要的属性就是StyleLookup,预置了一系列常用的图标,如下图: 另外2个常用属性就是: GroupName和IsPressed:一 ...