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. WebForm页面生命周期及asp.net运行机制

    1.先上几张原理图着重理解: 现在针对第四副图原理进行解析: 流程: 1.浏览器发送请求 2.服务器软件(IIS)接收,它最终的目的就是为了向客户输出它请求的动态页面生成的html代码. 3.服务器不 ...

  2. 用sql获取某字符串中的数字部分的语句

    create function dbo.F_Get_No ( @No varchar(100) ) RETURNS bigint AS BEGIN WHILE PATINDEX('%[^0-9]%', ...

  3. makeObjectsPerformSelector: 方法的用法

    //让数组中得每个元素都调用 @selector 中的get方法 [self.buttons makeObjectsPerformSelector:@selector(get)]; NSString ...

  4. KVC和KVO的区别

    kvc和kvo 1.kvc Key-Value Coding (KVC) KVC,即是指 NSKeyValueCoding,一个非正式的 Protocol,提供一种机制来间接访问对象的属性.KVO 就 ...

  5. Linux下tail命令

    简述 tail命令从指定点开始将文件写到标准输出,使用tail命令的“-f”选项可以方便的查阅正在改变的日志文件,“tail -f filename”会把filename里最尾部的内容显示在屏幕上,并 ...

  6. Linux/Unix shell sql 之间传递变量

    灵活结合Linux/Unix Shell 与SQL 之间的变量传输,极大程度的提高了DBA的工作效率,本文针对Linux/Unix shell sql 之间传递变量给出几个简单的示例以供参考. Lin ...

  7. 将UE添加到右键菜单

    1.新建UE.reg文件,将如下代码拷贝进去.注意UE安装路径 Windows Registry Editor Version 5.00  [HKEY_CLASSES_ROOT*shell]  [HK ...

  8. Windows下PHP+Eclipse开发环境搭建 及错误解决(apache2.2服务无法启动 发生服务特定错误:1)

    前言 Eclipse与php/apache的关系:Eclipse只是用来写代码的,如果想要在浏览器查看运行效果就要让php/apache的运行目录指向你的代码目录.Eclipse貌似不会自己和apac ...

  9. Eziriz.Net.Reactor使用注意事项

    1) 保护参数配置 2) 注册表访问 using System.Security.Permissions; [RegistryPermissionAttribute(SecurityAction.Pe ...

  10. ArcGlobe点击IGlobeServerLayer图层读取信息

    ArcGISServer将点图层发布成Globe服务,AE开发中自定义识别工具,读取点数据信息. 1) 通过Locate方法获取图层对象,图层对象中的SearchOID就是你点中的要素Objectid ...