There  are two sorted arrays A and B 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)).

这个题是求两个已排数组的中位数,更一般的就是求两个已排数组的第k大的数。

自己想到的解决方法有一个先用合并排序将两个数组进行排序得到一个排列后的数组,在遍历找到第k到的数,但是这样算法的复杂度是o(nlogn),显然不符合题目的要求。

参考http://blog.csdn.net/zxzxy1988/article/details/8587244中的解法:

假设A,B两个数组元素都大于k/2。可以比较A[k/2]和B[k/2]

(1)A[k/2-1] < B[k/2-1] 此时可以判断A[0]~A[k/2]均比最终要找的median要小,可以去掉这部分元素。

(2)A[k/2-1] > B[k/2-1]此时可以判断B[0]~B[k/2]均比最终要找的median要小,可以去掉这部分元素。

(3)A[k/2-1] = B[k/2-1]此时判断要找的median就是A[k/2-1]orB[k/2-1].

使用上面的结论,还要考虑边界情况。

实现代码如下

class Solution {
public:
double findMedianSortedArrays(int a[], int m, int b[], int n)
{
int total = m + n;
if(total & 0x1)
{
return findkth(a, m, b, n, total / 2 + 1);
}
else
{
return (findkth(a, m, b, n, total / 2) +findkth(a, m, b,n, total /2 + 1)) / 2;
}
}
double findkth(int a[], int m, int b[], int n, int k)
{
if(m > n)
return findkth(b, n, a, m, k);
if(m == 0)
return b[k-1];
if(k <= 1)
return min(a[0], b[0]);
int idxA = min(k/2, m);
int idxB = k - idxA;
if(a[idxA - 1] < b[idxB-1])
{
return findkth(a+idxA, m-idxA, b, n, k- idxA);
}
else if(b[idxB-1] < a[idxA-1])
{
return findkth(a, m, b+ idxB, n - idxB, k - idxB );
}
else
return a[idxA - 1];
}
};

  使用的迭代,考虑到求解的方便使用:

if(m > n)
return findkth(b, n, a, m, k);//这句使得后面具体结果讨论省了一半,32个赞
if(m == 0)
return b[k-1];
if(k <= 1)
return min(a[0], b[0]);

对边界情况进行讨论。

多看多学习,不管怎样,这是个好时代,加油。

LeetCode 4 Median of Two Sorted Array的更多相关文章

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

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

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

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

  3. [LeetCode] 033. Search in Rotated Sorted Array (Hard) (C++)

    指数:[LeetCode] Leetcode 解决问题的指数 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 033. ...

  4. [array] leetcode - 33. Search in Rotated Sorted Array - Medium

    leetcode - 33. Search in Rotated Sorted Array - Medium descrition Suppose an array sorted in ascendi ...

  5. LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++>

    LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++> 给出排序好的一维有重复元素的数组,随机取一个位置断开 ...

  6. LeetCode 33 Search in Rotated Sorted Array [binary search] <c++>

    LeetCode 33 Search in Rotated Sorted Array [binary search] <c++> 给出排序好的一维无重复元素的数组,随机取一个位置断开,把前 ...

  7. LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>

    LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...

  8. LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] <c++>

    LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] <c++> 给出排序好的 ...

  9. [LeetCode] 80. Remove Duplicates from Sorted Array II ☆☆☆(从有序数组中删除重复项之二)

    https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/discuss/27976/3-6-easy-lines-C% ...

随机推荐

  1. Linux下创建pycharm的快捷方式

    第一步:创建桌面快捷方式文件Pycharm.desktop,并打开 sudo gedit /usr/share/applications/Pycharm.desktop 第二步:在打开的文件Pycha ...

  2. Freemarker 的 Shiro 标签使用详解

    一.引入依赖(已解决版本冲突) <!-- shiro-freemarker-tags start --> <dependency> <groupId>net.min ...

  3. MySQL☞聚合函数/分组函数

    分组函数(聚合函数) 1.count(*/列名): a.*:求出该数据的总条数 select  count(*)  from 表名 b.列名:求出该列中列名不为null的总条数 select  cou ...

  4. CCF-NOIP-2018 提高组(复赛) 模拟试题(五)

    T1 相遇 [问题描述] 在一场奇怪的梦里,小 Y 来到了一个神奇的国度.这个国度可以用一根数轴表示,小 Y 在 N 处,而小 Y 想吃的美食在 K 处.小 Y 有两种方式移动, 一种叫做步行, 一种 ...

  5. Paper Reading - Show and Tell: Lessons learned from the 2015 MSCOCO Image Captioning Challenge

    Link of the Paper: https://arxiv.org/abs/1609.06647 A Correlative Paper: Show and Tell: A Neural Ima ...

  6. day-9 sklearn库和python自带库实现最近邻KNN算法

    K最近邻(k-Nearest Neighbor,KNN)分类算法,是一个理论上比较成熟的方法,也是最简单的机器学习算法之一.该方法的思路是:如果一个样本在特征空间中的k个最相似(即特征空间中最邻近)的 ...

  7. POJ 1144 Network(割点)

    Description A Telephone Line Company (TLC) is establishing a new telephone cable network. They are c ...

  8. mysql 数据库新增用户

    1.user表中host为%含义: Host列指定了允许用户登录所使用的IP,比如user=root Host=192.168.1.1.这里的意思就是说root用户只能通过192.168.1.1的客户 ...

  9. Java的同步容器和并发容器

    前言: 之前在介绍Java集合的时候说到,java提供的实现类很少是线程安全的.只有几个比较古老的类,比如Vector.Hashtable等是线程安全的,尤其是Hashtable,古老到连命名规范都没 ...

  10. charles和Fiddler感觉哪个更好用

    1.fiddler还可以抓HTTPS的包,解析出来都可以     2.charles更直观,可能是我先用charles的缘故.charles遍历一个站点,可以右键另存,保存全站文件资源.扒站首选, c ...