题目描述

有两个大小分别为m和n的有序数组A和B。请找出这两个数组的中位数。你需要给出时间复杂度在O(log (m+n))以内的算法。

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)).
示例1

输入

[],[1]

/归并数组,但是不需要完全估计,只需要合并到中位数即可
//但是,如果m+n为偶数,那么返回中间两个值的平均数
class Solution {
public:
    double findMedianSortedArrays(int A[], int m, int B[], int n) {
        int mid=(m+n)/2+1;
        int a=0;
        int b=0;
        int result;
        int result2=0;
        for(int i=0;i<mid;i++){
          //复杂度为(m+n)/2+1,也就是m+n
            if (b>=n || (a<m && A[a]<B[b])){
                result=result2;
                result2=A[a];
                a++;
            }
            else {
                result=result2;
                result2=B[b];
                b++;
            }
        }
        if ((m+n)%2==0)  return double(result+result2)/2.0;
        return result2;
    }
};

/*
** 获取两个数组的中位数
*/
 public double findMedianSortedArrays(int[] A, int[] B) {
       int m = A.length;//数组A的长度
       int n = B.length;//数组B的长度
       int l = (m+n+1)/2;
       int r = (m+n+2)/2;
       
       /*取两个数的平均值,即适用于总长度m+n是奇数的情况,也适用于是偶数的情况。
       * 奇数时,两次调用所获得的值相等;
       偶数时,两次调用所获得的值不等。中位数即为两个值的平均值*/
       return (getKth(A,0,B,0,l)+getKth(A,0,B,0,r))/2.0;
    }
    
 
    /*获取数组A和数组B结合后的第k小元素(即中位数)
    * s1:数组A当前的开始下标
    * s2:数组B当前的开始下标
    */
    private int getKth(int[] A, int s1, int[] B, int s2, int k){
        
 
        if(A.length==0){//1.数组A为空,返回数组B的中位数
            return B[s2+k-1];
        }
        if(B.length==0){//2.数组B为空,返回数组A的中位数
            return A[s1+k-1];
        }
        if(k==1){
            return Math.min(A[s1],B[s2]);
        }
 
        
        //4.A和B都有多个元素
        /*在数组A中找到第k/2小元素a,在数组B中找到第k/2小元素b,
        **1)如果a和b相等,那么第k小元素就是a或者b了,
        **2)如果a小于b,那么总体的第k小元素不可能在a的第k/2小元素之前,那么就可以将其舍弃了
        **3)反之如果a大于b,也就可以舍弃b的第k/2小之前的元素了。*/
        int mida = Integer.MAX_VALUE;
        int midb = Integer.MAX_VALUE;
        
        if(s1+k/2-1<A.length){
            mida = A[s1+k/2-1];
        }
        if(s2+k/2-1<B.length){
            midb = B[s2+k/2-1];
        }
        if(mida<midb){//去除A中小的部分,继续递归寻找
            return getKth(A,s1+k/2,B,s2,k-k/2);
        }else{//即mina>minb 去除B中小的部分,继续递归寻找
            return getKth(A,s1,B,s2+k/2,k-k/2);
        }
    }

class Solution:
    def findMedianSortedArrays(self , A , B ):
        # write code here
        arr = sorted(A + B)
        if len(arr)%2 == 1:
            return (arr[(len(arr)/2)]*1.0)
        if len(arr)%2 == 0:
            return ((arr[(len(arr)/2)] + arr[((len(arr)/2)-1)])/2.0)

leetcode147median-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 两个有序数组的中位数

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

  3. 【leetcode】Median of Two Sorted Arrays

    题目简述: There are two sorted arrays A and B of size m and n respectively. Find the median of the two s ...

  4. 【leedcode】 Median of Two Sorted Arrays

    https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 and num ...

  5. leetcode-【hard】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 ...

  6. Leetcode4:Median of Two Sorted Arrays@Python

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

  7. leetcode 4. Median of Two Sorted Arrays

    https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 and num ...

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

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

  9. Leetcode Median of Two Sorted Arrays

    There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted ...

  10. [LintCode] Median of Two Sorted Arrays 两个有序数组的中位数

    There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted ...

随机推荐

  1. Ubuntu通过iptables配置 ip 代理转发

    开启 ip 代理转发 临时开启 ip 代理转发 # 执行该命令后立即生效,但是重启后会失效 echo 1 | sudo tee /proc/sys/net/ipv4/ip_forward 永久开启 i ...

  2. Recursive sequence (矩阵快速幂)2016ACM/ICPC亚洲区沈阳站

    题目 Farmer John likes to play mathematics games with his N cows. Recently, they are attracted by recu ...

  3. C# Socket TCP发送图片与接收图片

    如果需要查看更多文章,请微信搜索公众号 csharp编程大全,需要进C#交流群群请加微信z438679770,备注进群, 我邀请你进群! ! ! --------------------------- ...

  4. 使用appium后安卓手机无法调出键盘解决方法

    问题:用appium进行真机调试后,使用手机的app进行输入时无法调出键盘. 原因:appium调试时,将手机输入法设置成了Unicode IME 解决方法: 方法一,手机设置里修改输入法: 不同的手 ...

  5. Spark 单机环境配置

    概要 Spark 单机环境配置 JDK 环境配置 Spark 环境配置 python 环境配置 Spark 使用示例 示例代码 (order_stat.py) 测试用的 csv 文件内容 (order ...

  6. 从面试角度学完 Kafka

    Kafka 是一个优秀的分布式消息中间件,许多系统中都会使用到 Kafka 来做消息通信.对分布式消息系统的了解和使用几乎成为一个后台开发人员必备的技能.今天码哥字节就从常见的 Kafka 面试题入手 ...

  7. golang通过cgo调用lua

    目录 1.前期准备 2.测试go代码 3.完成的一个学习项目 4.总结 1.前期准备 1.第三方库:https://github.com/aarzilli/golua 2.下载lua源码:https: ...

  8. 新手学习C/C++编程过程中常见的那些坑,一定要多多注意!

    C/C++中的指针让程序员有了更多的灵活性,但它同时也是一把双刃剑,如果用的不好,则会让你的程序出现各种各样的问题,有人说,C/C++程序员有一半的工作量是花在处理由指针引起的bug上,可想而知,指针 ...

  9. 不可不知的资源管理调度器Hadoop Yarn

    Yarn(Yet Another Resource Negotiator)是一个资源调度平台,负责为运算程序如Spark.MapReduce分配资源和调度,不参与用户程序内部工作.同样是Master/ ...

  10. 更换Centos的yum源

    1.备份 mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup 2.下载新的CentOS-Base ...