leetcode147median-of-two-sorted-arrays

题目描述

有两个大小分别为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)
posted on 2020-07-18 07:10  滚雪球效应  阅读(136)  评论(0编辑  收藏  举报