第9题 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 arrays. The overall run time complexity should be O(log (m+n)).

Note:求两个sorted array的中间值,不是平均值哦。

Solution1: 先根据两个array的长度找到中间的位置,若总长为偶数,中间值为一个element的值,若总长为偶数,则中间值为两个正中元素的平均值,所以返回参数是double。程序中注意一些细节的判断和极端情况的考虑。如果其中一个数组长度为0呢?如果在判断的过程中数组下标超过了数组范围呢?给数组A和B各分配一个指针,每一次count数组A和B指针所指的较小的值,然后指针后移,直到到达计算出的中间位置。

这种方法不需要额外开辟内存。只记录中间的数值即可。

public class Solution {
    public double findMedianSortedArrays(int A[], int B[]) {
        double median=0;
        int m = A.length;
        int n = B.length;
        if(m==0 &&n==0) return 0;
        int position=0;
        boolean even = false;
        if( (m+n)%2==0) {even =true;    position = (m+n)/2;}
        else    {position = (int)((m+n)/2)+1;   }
        int posA=0, posB=0, posAB=0, mid=0;
        while(posAB<position){
            if(posA>=m){  
                mid=B[posB];
               posB++;
               posAB++;
                
            }
            else if(posB>=n){
               mid=A[posA];
               posA++;
               posAB++;
            }
           else if( A[posA]>=B[posB] ){
               mid=B[posB];
               posB++;
               posAB++;
              
           }
          else{
               mid=A[posA];
               posA++;
               posAB++;
           }
        }
        if(!even)
            return median=mid;
        else{
            if(posA<m &&posB<n)
                return median=  (double)(mid +Math.min(A[posA], B[posB]))/2;
            else if(posA>=m) return median = (double)(mid+B[posB])/2;
            else return median = (double)(mid+A[posA])/2;
        }
            
        
    }
}

Solution2: 利用一个ArrayList暂存merged array

public class Solution {
    public double findMedianSortedArrays(int A[], int B[]) {
        if(A.length==0 && B.length==0) return 0;
       ArrayList<Integer>  merged = new ArrayList<Integer>();
       int ptrA = A.length-1;
       int ptrB = B.length-1;
       while(ptrA>=0 && ptrB>=0){
           if(A[ptrA]>=B[ptrB]) {
               merged.add(A[ptrA]);
               ptrA--;
           }
           else{
               merged.add(B[ptrB]);
               ptrB--;
           }
       }
       while(ptrB>=0){
           merged.add(B[ptrB]);
           ptrB--; 
       }
       while(ptrA>=0){
           merged.add(A[ptrA]);
           ptrA--; 
       }
        int length = merged.size();
        return (merged.get(length>>1) + merged.get((length-1)>>1) )/2.0;
        
    }
}

ArrayList中元素是按从大到小排列。但由于是求居中元素的值,所以没有影响。

“>>”符号为按位右移。如果length是奇数的话,只应返回居中一个元素的值,这时候length>>1和length-1>>1的结果未完全一样的,都是居中元素的坐标。

当length为偶数时,应返回居中两元素的平均数。这时length>>1和length-1>>1分别为居中两元素的下坐标。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值