题目描述

有两个大小分别为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. Activity常用方法

    setContentView(r.layout.xxxx);//设置布局文件 getViewById(r.id.xxxx);//获取指定控件 getString(r.string.xxxx);//获取 ...

  2. 从字节码层次看i++和++i

    关于的Java的i++和++i的区别,初学者可能会混淆,这时候有经验的同学或同事就会告诉你,++在后,就会立马加值, ++在后则会等会儿再加,所以如果i == 0 ,那么i++ == 0,++i == ...

  3. LR Optimization-Based Estimator Design for Vision-Aided Inertial Navigation

    Abstract 我们设计了一个 hybrid 估计器, 组合了两种算法, sliding-window EKF 和 EKF-SLAM. 我们的结果表示, hybrid算法比单一的好. 1. Intr ...

  4. 游戏2048的核心算法c#版本的实现

    接触游戏有一段时间了,也写了一些东西,效果还不错,今天没事,我就把2048 c# 版本的实现贴出来,代码已经测试过,可以正常.完美运行.当然了,在网上有很多有关2048的实现方法,但是没有提出到类里面 ...

  5. java流程控制之习题

     经过近段时间的学习,差不多也掌握了java的流程控制以及基本知识,下面就来一起练练习题吧,看能做出来几道. 第一道题:假设小明有100块钱,这时候小明去超市需要换零钱,超市提供的零钱有1元面值,2元 ...

  6. doment ready事件和load事件的区别及实现

    从2017年9月至今工作中大部分使用react,前端渲染的一些基础性知识记忆不是很深刻了.面试**公司的时候,碰到"document ready和load的区别,以及document rea ...

  7. 后羿:我射箭了快上—用MotionLayout实现王者荣耀团战

    前言 昨晚跟往常一样,饭后开了一局王者荣耀,前中期基本焦灼,到了后期一波决定胜负的时候,我果断射箭,射中对面,配合队友直接秒杀,打赢团战一波推完基地.那叫一个精彩,队友都发出了666666的称赞,我酷 ...

  8. git学习(八) git stash操作

    git stash命令的作用就是将目前还不想提交的但是已经修改的内容进行保存至堆栈中,后续可以在某个分支上恢复出堆栈中的内容.git stash作用的范围包括工作区和暂存区中的内容,没有提交的内容都会 ...

  9. Spring+Hibernate+Struts2整合之实现登录功能

    前端代码: <form id="loginForm" action="${ pageContext.request.contextPath }/user_login ...

  10. java中继承和多态

    转自原文http://blog.csdn.net/xinxin19881112/article/details/2944760 若冒犯博主,请勿见怪! 1.  什么是继承,继承的特点? 子类继承父类的 ...