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

解题思路:

该题可以解决所有求有序数组A和B有序合并之后第k小的数!

该题的重要结论:

如果A[k/2-1]<B[k/2-1],那么A[0]~A[k/2-1]一定在第k小的数的序列当中,可以用反证法证明。

具体的分析过程可以参考http://blog.csdn.net/zxzxy1988/article/details/8587244

class Solution {
public:
double findKth(int A[], int m, int B[], int n, int k)
{
//m is equal or smaller than n
if (m > n)
return findKth(B, n, A, m, k);
if (m == )
return B[k-];
if (k <= )
return min(A[], B[]); int pa = min(k / , m), pb = k - pa;
if (A[pa-] < B[pb-])
{
return findKth(A + pa, m - pa, B, n, k - pa);
}
else if(A[pa-] > B[pb-])
{
return findKth(A, m, B + pb, n - pb, k - pb);
} else
return A[pa-];
} double findMedianSortedArrays(int A[], int m, int B[], int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int k = m + n;
if (k & 0x1)
{
return findKth(A, m, B, n, k / + );
} else
{
return (findKth(A, m, B, n, k / ) + findKth(A, m, B, n, k / + )) / ;
}
}
};

Median of Two Sorted Arrays-----LeetCode的更多相关文章

  1. Median of Two Sorted Arrays LeetCode Java

    两排序好的数组,找中位数 描述There are two sorted arrays A and B of size m and n respectively. Find the median of ...

  2. 3.Median of Two Sorted Arrays Leetcode

    难度系数:5星 /*************************************************************************** 题目:有两个排好序的数组,要求 ...

  3. 4. Median of Two Sorted Arrays(topK-logk)

    4. Median of Two Sorted Arrays 题目 There are two sorted arrays nums1 and nums2 of size m and n respec ...

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

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

  5. LeetCode 4 Median of Two Sorted Arrays (两个数组的mid值)

    题目来源:https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 an ...

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

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

  7. Kotlin实现LeetCode算法题之Median of Two Sorted Arrays

    题目Median of Two Sorted Arrays(难度Hard) 方案1,数组合并&排序调用Java方法 import java.util.* class Solution { fu ...

  8. Leetcode 4. Median of Two Sorted Arrays(二分)

    4. Median of Two Sorted Arrays 题目链接:https://leetcode.com/problems/median-of-two-sorted-arrays/ Descr ...

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

  10. LeetCode 4. Median of Two Sorted Arrays & 归并排序

    Median of Two Sorted Arrays 搜索时间复杂度的时候,看到归并排序比较适合这个题目.中位数直接取即可,所以重点是排序. 再来看看治阶段,我们需要将两个已经有序的子序列合并成一个 ...

随机推荐

  1. 漫游Kafka实战篇之搭建Kafka运行环境

    接下来一步一步搭建Kafka运行环境. Step 1: 下载Kafka 点击下载最新的版本并解压. > tar -xzf kafka_2.9.2-0.8.1.1.tgz > cd kafk ...

  2. A*寻路初探 GameDev.net 转载

    A*寻路初探 GameDev.net 译者序:很久以前就知道了A*算法,但是从未认真读过相关的文章,也没有看过代码,只是脑子里有个模糊的概念.这次决定从头开始,研究一下这个被人推崇备至的简单方法,作为 ...

  3. HDU 5273 Dylans loves numbers(水题)

    题意:给出一个0≤N≤1018,求其二进制中有几处是具有1的,假设相连的1只算1处,比如1101011就是3处. 思路:一个个数,当遇到第一个1时就将flag置为1:当遇到0就将flag置为0.当遇到 ...

  4. DelegatingFilterProxy

    安全过滤器链 Spring Security的web架构是完全基于标准的servlet过滤器的. 它没有在内部使用servlet或任何其他基于servlet的框架(比如spring mvc), 所以它 ...

  5. MatrixTurn源码阅读

    在看cacheAsBitmap 相关资料时,找到bit101的一篇文章,http://www.bytearray.org/?p=290 全文如下: One of the feature I would ...

  6. 用Python抓网页的注意事项

    用Python编一个抓网页的程序是非常快的,下面就是一个例子: import urllib2 html = urllib2.urlopen('http://blog.raphaelzhang.com' ...

  7. MySQL与Oracle 差异比较之五存储过程&Function

    存储过程&Function 编号 类别 ORACLE MYSQL 注释 1 创建存储过程语句不同 create or replace procedure P_ADD_FAC(   id_fac ...

  8. gcc-4.8.3安装,gdb-7.6安装

    gdb用法: http://blog.chinaunix.net/uid-26548237-id-3435525.html gdb-7.6.tar.gz:  (官网下载:http://ftp.gnu. ...

  9. sessionFactory.getCurrentSession()的引出

    当业务逻辑中需要开启事务执行,业务逻辑也要调用底层操作数据库的函数,那函数也要开启事务操作. 如果用sessionFactory.openSession()的话会引起处理不在同一个事务中,会造成出错. ...

  10. mysql 用户名密码登陆不上

    问题1:刚安装完mysql,设置了用户名密码root,登陆OK的,后来再连怎么也连不上了 操作步骤: 输入:mysql -uroot -proot 提示:ERROR 1045 (28000): Acc ...