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. i8浏览器不支持placeholder属性解决办法,以及解决后,文字不居中问题

    这里想实现的效果是:设置和移除文本框默认值,如下图鼠标放到文本框中的时候,灰字消失. 1.可以用简单的方式,就是给input文本框加上onfocus属性,如下代码: <input id=&quo ...

  2. Python全栈 正则表达式(概念、、语法、元字符、re模块)

    前言:        普通人有三件东西看不懂:医生的处方,道士的鬼符,程序员得正则表达式       什么是正则表达式? 正则表达式,又称规则表达式,英文名为Regular Expression,在代 ...

  3. Java并发基础--线程安全

    一.线程安全 1.线程安全的概念 线程安全:某个类被单个线程,或者多个线程同时访问,所表现出来的行为是一致,则可以说这个类是线程安全的. 2.什么情况下会出现线程安全问题 在单线程中不会出现线程安全问 ...

  4. 给移动硬盘安装rhel7

    本机是win8.1的系统,但不想给电脑装双系统,所以想给移动硬盘里安装rhel7移动硬盘是750G的在网上搜了很多方法,我采取了两个方法:方法一.1.取一个U盘,用软碟通把rhel7的iso文件写进了 ...

  5. 九度OJ--Q1473

    import java.util.ArrayList;import java.util.Scanner; /* * 题目描述: * 大家都知道,数据在计算机里中存储是以二进制的形式存储的. * 有一天 ...

  6. web相关基础知识1

    2017-12-13 09:47:11 关于HTML 1.绝对路径和相对路径 相对路径:相对于文件自身为参考. (工作中一般是使用相对路径) 这里我们用html文件为参考.如果说html和图片平级,那 ...

  7. Nginx代理实现跨域

    #user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #erro ...

  8. 【bzoj1018】[SHOI2008]堵塞的交通traffic 线段树区间合并+STL-set

    题目描述 给出一张2*n的网格图,初始每条边都是不连通的.多次改变一条边的连通性或询问两个点是否连通. 输入 第一行只有一个整数C,表示网格的列数.接下来若干行,每行为一条交通信息,以单独的一行“Ex ...

  9. Numpy array学习笔记

  10. 利用FFT来进行字符串匹配

    给定串A和串B,A由26个小写字母构成,B由?和26个小写字母构成 ?可以和任意字符匹配 求A中出现了多少次B 这里可以使用fft做法,定义向量A和向量B 然后求A和rev(B)的卷积结果C C的第i ...