题目:

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

题意已只两个有序的序列,找到他们的中位数,复杂度要求O(log (m+n))。

问题可以转化成两个有序序列找第num大的数,用类似二分的思想,用递归处理。

因为两个序列是有序的,对比A和B第num/2个数大小,每次把小的序列删掉num/2个数,能保证不会删掉第num大的数,可以纸上验证一下。

如果一个序列没有num/2个数,那么就比较两个序列第min(n,m)个数的大小,这么做能尽快删掉一个序列所有元素,结束递归。

这么做最坏情况复杂度是O(log (m+n)),也就是num递归到1。

double find(int A[],int m,int B[],int n,int del)
{
if(m==0)return B[del-1];
else if(n==0)return A[del-1];
else if(del==1)return A[0]<B[0]?A[0]:B[0];
int temp=del/2;
if(min(m,n)<temp)temp=min(m,n);
if(A[temp-1]>=B[temp-1])return find(A,m,B+temp,n-temp,del-temp);
else return find(A+temp,m-temp,B,n,del-temp);
} class Solution {
public:
double findMedianSortedArrays(int A[], int m, int B[], int n) {
int del=(n+m+1)/2;
double temp=find(A,m,B,n,del);
if((m+n)&1)return temp;
else return (find(A,m,B,n,del+1)+temp)/2.0;
}
};

Median of Two Sorted Arrays (找两个序列的中位数,O(log (m+n))限制) 【面试算法leetcode】的更多相关文章

  1. 【LeetCode每天一题】Median of Two Sorted Arrays(两数组中的中位数)

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

  2. 《LeetBook》leetcode题解(4): Median of Two Sorted Arrays[H]——两个有序数组中值问题

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  3. leetcode 4 : Median of Two Sorted Arrays 找出两个数组的中位数

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

  4. 【LeetCode】4. Median of Two Sorted Arrays 寻找两个正序数组的中位数

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:数组,中位数,题解,leetcode, 力扣,python ...

  5. 4. Median of Two Sorted Arrays[H]两个有序数组的中位数

    题目 There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the midian of the ...

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

    Level:   Hard 题目描述: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find t ...

  7. 4. Median of Two Sorted Arrays(2个有序数组的中位数)

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

  8. LeetCode Median of Two Sorted Arrays 找中位数(技巧)

    题意: 给两个有序(升or降)的数组,求两个数组合并之后的中位数. 思路: 按照找第k大的思想,很巧妙.将问题的规模降低,对于每个子问题,k的规模至少减半. 考虑其中一个子问题,在两个有序数组中找第k ...

  9. leetcode 4. Median of Two Sorted Arrays 寻找两个正序数组的中位数(困难)

    一.题目大意 标签: 查找 https://leetcode.cn/problems/median-of-two-sorted-arrays 给定两个大小分别为 m 和 n 的正序(从小到大)数组 n ...

随机推荐

  1. 沙湖王 | 用K-均值聚类给女明星们的身材分分类

    沙湖王 | 用K-均值聚类给女明星们的身材分分类 http://www.shahuwang.com/2012/07/21/%E7%94%A8scipy%E5%AE%9E%E7%8E%B0k-means ...

  2. JS中简单的this学习

        我在学习JS初期,在使用this的时候经常出现问题,当然就是在现在,也有一些场景不能很好的明白this到底指代的是什么?看下面一个例子:   var x = 10; var foo = { x ...

  3. linux下面测试网络带宽 (转载)

    利用bmon/nload/iftop/vnstat/iptraf实时查看网络带宽状况 一.添加yum源方便安装bmon# rpm -Uhv http://apt.sw.be/redhat/el5/en ...

  4. (转)Eclipse 远程调试 WebSphere Application Server (WAS)

    目前我们项目中使用的应用服务器多是WebSphere,一直苦于无法进行调试,今天在网上看到一篇,原文是 http://www.cnblogs.com/newstar/archive/2010/04/1 ...

  5. html5 音频

    目前,web页面上没有标准的方式来播放音频文件,大多数的音频文件是使用插件来播放,而众多的浏览器使用了不同的插件.而html5的到来,给我们提供了一个标准的方式来播放web中音频文件,用户不再为浏览器 ...

  6. css中的伪类

    伪类用于向某些选择器添加一些特殊效果. 1):focus 伪类在元素获得焦点的时向元素添加特殊样式.一般用于输入文本域,按钮,以及超链接. a:focus{color:red;}超链接字体为红色 in ...

  7. ArcGIS10.3.1于2015年6月发布

    http://www.esrichina.com.cn/sectorapplication/ArcGIS%2010.3/index.html

  8. IOS开发几何类方法 CGGeometry.h文件

    CGGeometry.h文件是用C语言实现的一个封装了许多常用几何方法的文件. 一.几个常用结构体 struct CGPoint { CGFloat x; CGFloat y; }; 定义一个点,设置 ...

  9. MySQL 基础 之 语句执行顺序

    FORM: 对FROM的左边的表和右边的表计算笛卡尔积.产生虚表VT1 ON: 对虚表VT1进行ON筛选,只有那些符合<join-condition>的行才会被记录在虚表VT2中. JOI ...

  10. Performing Post-Build Event之类的编译错误

    如果编译出现Perror PRJ0019: A tool returned an error code from "Performing Post-Build Event..."之 ...