题目描述

有两个大小分别为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. Spring Boot+Spring Security+JWT 实现 RESTful Api 认证(一)

    标题 Spring Boot+Spring Security+JWT 实现 RESTful Api 认证(一) 技术 Spring Boot 2.Spring Security 5.JWT 运行环境 ...

  2. 在nginx下导出数据库数据

    首先上干货 解决问题 set_time_limit(0); //设置脚本运行时间为不限制 因为php脚本默认时间为30秒 ini_set('memory_limit', -1); //取消脚本运行内存 ...

  3. 多测师讲解html _无序列表006_高级讲师肖sir

    <!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>无 ...

  4. Springboot项目集成JPush极光推送(Java SDK)

    1.由于项目的需求,需要在Android APP上实现消息推送功能,所以引用了极光推送(官网:https://www.jiguang.cn/, 文档:http://docs.jiguang.cn/) ...

  5. SpringBoot常见注解

    0.前言 这篇文章介绍的 Spring/SpringBoot 常用注解基本已经涵盖你工作中遇到的大部分常用的场景.对于每一个注解我都说了具体用法,掌握搞懂,使用 SpringBoot 来开发项目基本没 ...

  6. 移动吉比特H2-2光猫超级用户与密码

    移动吉比特H2-2光猫超级用户与密码 超级用户名CMCCAdmin 密码aDm8H%MdA----------------版权声明:本文为CSDN博主「BenSon.Album」的原创文章,遵循CC ...

  7. Spark如何进行动态资源分配

    一.操作场景 对于Spark应用来说,资源是影响Spark应用执行效率的一个重要因素.当一个长期运行的服务,若分配给它多个Executor,可是却没有任何任务分配给它,而此时有其他的应用却资源紧张,这 ...

  8. vue-awesome-swiper ---移动端h5 swiper 和 tab 栏选项联动效果实现

    很久之前做小程序时有个类似每日优鲜里储值卡充值界面里的 卡轮播和价格tab栏联动效果,当时觉得新鲜做出来之后也没当回事.直到今天又遇到了一个类似的功能,所以想着总结经验. 实现效果如下图: 图解:点击 ...

  9. Java安全之Commons Collections2分析

    Java安全之Commons Collections2分析 首发:Java安全之Commons Collections2分析 0x00 前言 前面分析了CC1的利用链,但是发现在CC1的利用链中是有版 ...

  10. c++ 遍历目录下文件、文件夹

    BOOL GetDirFiles(const char* pszDir, char* pszFileType, std::vector<std::string>& vtFileLi ...