题目:

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

解题思路:

这道题,很容易想到的是先排序再直接定位中间那个数即可,m+n为偶数的话,应为中间两数之和除2,但时间复杂度不符合题目要求,还一种方法就是利用归并思想,因为两数组为排序数组,对两数组进行归并,当已归并个数为(m+n)/2时,即求得中间数,不用继续归并了,不过这种方式时间复杂度也不符合要求。那有没其他更快的方法呢。

这里要说的就是利用二分查找的方式,当要在排序数组中查找某个元素时,我们就应该朝二分法查找思考。二分法正是利用已排序数组这一特性快速查找。该方法的核心是将原问题转变成一个寻找第k小数的问题(假设两个原序列升序排列),这样中位数实际上是第(m+n)/2小的数。所以只要解决了第k小数的问题,原问题也得以解决。首先假设数组A和B的元素个数都大于k/2,我们比较A[k/2-1]和B[k/2-1]两个元素,这两个元素分别表示A的第k/2小的元素和B的第k/2小的元素。这两个元素比较共有三种情况:>、<和=。如果A[k/2-1]<B[k/2-1],这表示A[0]到A[k/2-1]的元素都在A和B合并之后的前k小的元素中。换句话说,A[k/2-1]不可能大于两数组合并之后的第k小值,所以我们可以将其抛弃。

实现代码:

#include <iostream>

using namespace std;

/**
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)).
*/
class Solution {
public:
double findMedianSortedArrays(int A[], int m, int B[], int n) {
int k = (m + n) / 2;
if((m + n) & 0x1)//奇数
return findKthNumber(A, m, B, n, k + 1);
return (findKthNumber(A, m, B, n, k) + findKthNumber(A, m, B, n, k+1)) / 2; }
double findKthNumber(int A[], int m, int B[], int n, int k)
{
if(m > n)
return findKthNumber(B, n, A, m, k);//这里始终假设m<n
if(m == 0)
return B[k-1];
if(n == 0)
return A[k-1];
if(k == 1)
return A[0] < B[0] ? A[0] : B[0];
int ak = min(k / 2, m);
int bk = k - ak;
if(A[ak-1] < B[bk-1])
return findKthNumber(A+ak, m-ak, B, n, k-ak);
else
return findKthNumber(A, m, B+bk, n-bk, k-bk); }
};
int main(void)
{
int A[] = {1,5,7,9,14,15};
int B[] = {3,6,10,13,18,20};
Solution s;
double res = s.findMedianSortedArrays(A, 6, B, 6);
cout<<res<<endl;
return 0;
}

LeetCode2:Median of Two Sorted Arrays的更多相关文章

  1. No.004:Median of Two Sorted Arrays

    问题: There are two sorted arrays nums1 and nums2 of size m and n respectively.Find the median of the ...

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

  3. Q4:Median of Two Sorted Arrays

    4. Median of Two Sorted Arrays 官方的链接:4. Median of Two Sorted Arrays Description : There are two sort ...

  4. LeetCode第[4]题(Java):Median of Two Sorted Arrays 标签:Array

    题目难度:hard There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median ...

  5. 【LeetCode算法题库】Day2:Median of Two Sorted Arrays & Longest Palindromic Substring & ZigZag Conversion

    [Q4] There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of th ...

  6. 4:Median of Two Sorted Arrays

    here are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...

  7. LeetCode第[4]题(Java):Median of Two Sorted Arrays (俩已排序数组求中位数)——HARD

    题目难度:hard There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median ...

  8. leetcode 4:Median of Two Sorted Arrays

    public double FindMedianSortedArrays(int[] nums1, int[] nums2) { int t=nums1.Length+nums2.Length; in ...

  9. 2.Median of Two Sorted Arrays (两个排序数组的中位数)

    要求:Median of Two Sorted Arrays (求两个排序数组的中位数) 分析:1. 两个数组含有的数字总数为偶数或奇数两种情况.2. 有数组可能为空. 解决方法: 1.排序法 时间复 ...

随机推荐

  1. 阅读《Effective C++》系列

    <Effective C++>条款07:为多态基类声明virtual析构函数 这样做主要是为了防止内存泄漏,见我hexo博客. C++的虚析构函数 <Effective C++> ...

  2. C# Use Pop3Client to read gmail

    host = "pop.gmail.com" user = "xxxxx@gmail.com" password = "xxxx" port ...

  3. (笔记)Linux内核学习(六)之并发和同步概念

    一 临界区和竞争条件 临界区:访问和操作共享数据的代码段. 竞争条件:多个执行线程处于同一个临界区中. 处于竞争条件:造成访问的数据或者资源不一致状态: 对资源i的访问:ProcessA和B访问后得到 ...

  4. CLR VIA

     标题  状态  内容 什么是CLR? 什么是托管模块? 托管模块由什么组成? .net代码的执行过程   http://www.cnblogs.com/aaa6818162/p/4726581.ht ...

  5. [转载]寻找两个有序数组中的第K个数或者中位数

    http://blog.csdn.net/realxie/article/details/8078043 假设有长度分为为M和N的两个升序数组A和B,在A和B两个数组中查找第K大的数,即将A和B按升序 ...

  6. 国内的maven镜像

    阿里云 <mirror> <id>alimaven</id> <name>aliyun maven</name> <url>ht ...

  7. ZooKeeper与Curator注册和监控

    Curator提供了对zookeeper客户端的封装,并监控连接状态和会话session,特别是会话session过期后,curator能够重新连接zookeeper,并且创建一个新的session. ...

  8. Unix sed实用教程开篇

    已经看了一段时间的Linux Shell编程了,也能完成一些基本的使用,为了加深理解,恰好看到了The Unix School的一个sed&awk教程,不是简单的命令参数堆积,而是一个相当实用 ...

  9. SQL Server 问题 1 - SQL Server encountered error 0x80070422/0x8007042d

    今天执行SQL Server 2014的full-text search 查询操作:select * from table where contains(summary, 'smith') 报出如下错 ...

  10. 编译升级php之路(5.5.7 到 5.5.37)

    为在一台旧服务器上能使用slim,共经历了: 1.安装composer(需要高版本php,原来是5.5.7) 2.升级php版本到5.5.37(编译出错,准备使用docker) 3.升级centos内 ...