题目:

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. QString转换为char*

    QString在Qt里相当于C++里的std::string,或者是C里的c style string.不过,QString跟编码相关,在低层想把一个QString发送出去相当麻烦,尤其对方用的不是Q ...

  2. 解决WCF 调用方未由服务器进行身份验证或消息包含无效或过期的安全上下文令牌

    错误描述: 1. WCF:调用方未由服务器进行身份验证 2. 无法处理消息.这很可能是因为操作“http://tempuri.org/ISCCLSvc/GetCarriersByWareHouse”不 ...

  3. 调起qq临时通话

    1.在qq推广的网站上的推广工具中将个人或者公司的qq确认为推广qq 2. 选择好显示的图片,将代码拷贝到页面上就可以了

  4. WEB服务器3--IIS7.0安装和配置

    安装Web服务器(IIS) 点击开始菜单->所有程序->管理工具->服务器管理器,启动服务器管理器,界面如下: 在服务器管理器中,选择角色,你将可以看到角色总视图. 点击添加角色,会 ...

  5. Socket介绍

    一 socket协议 Socket协议的形象描述 socket的英文原义是“孔”或“插座”.在这里作为4BDS UNIX的进程通信机制,取后一种意思.socket非常类似于电话插座.以一个国家级电话网 ...

  6. 简单实用的HTML代码

    简单实用的HTML代码 一.HTML各种命令的代码: 1.文本标签(命令) <pre></pre> 创建预格式化文本 <h1></h1> 创建最大的标题 ...

  7. java web移植 遇到Project facet Java version 1.7 is not supported

    在移植eclipse项目时,如果遇到 "Project facet Java version 1.7 is not supported." 项目中的jdk1.7不支持.说明项目是其 ...

  8. CDZSC_2015寒假新人(1)——基础 i

    Description “Point, point, life of student!” This is a ballad(歌谣)well known in colleges, and you mus ...

  9. python快速搭建WebServer

    #!/usr/bin/python import SimpleHTTPServer import SocketServer import os PORT = 7777 WEBDIR = "/ ...

  10. php 程序员的历程

    今天一朋友该找工作了. 问了我好多 我整理了下  希望有些帮助 以下内容纯属个人感觉如果有不恰当的地方请忽略.... 我做的是项目的包工 就是把销售拿下的项目整合后给我们实现功能. --------- ...