Median of Two Sorted Arrays(OO)

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. median为中位数,如果为偶,则为中间两数平均值

2. [],[2,3] 应该是2.5

3. [1] [1] 应该是1

4. [2,2] [2]应该是2

真是考察逻辑缜密程序

错误集:

1. 

for(i=0,j=0;i<m&&j<n;)//这里应该是&&,而不是逗号
2. 
return B[(m+n)/2 - m];//当为单并且走完一个数组时,返回的值,不能加1

3. 

当m+n为偶数时,得到numbb时可直接返回了

class Solution {
public:
    double findMedianSortedArrays(int A[], int m, int B[], int n)
    {
        if(m==0)
        {
            if(n%2==1)
                return B[n/2];
            else
                return (B[n/2-1]+B[n/2])/2.0;
        }
        if(n==0)
        {
            if(m%2==1)
                return A[m/2];
            else
                return (A[m/2-1]+A[m/2])/2.0;
        }
       double mediasum;
       int cnt=0;
       int i,j;
        int numba, numbb;
       if((m+n)%2==1)
       {
          for(i=0,j=0;i<m&&j<n;)
         {
           if(A[i]<=B[j])
           {
               cnt++;
               if(cnt==(m+n)/2+1)
                return A[i];
                i++;
           }
           else
           {
               cnt++;
               if(cnt==(m+n)/2+1)
                return B[j];
                j++;
           }
         }
         if(i==m)
            return B[(m+n)/2 - m];
         if(j==n)
            return A[(m+n)/2 - n];
       }
      
       else
       {
           for(i=0,j=0;i<m&&j<n;)
         {
           if(A[i]<=B[j])
           {
               cnt++;
               if(cnt==(m+n)/2)
                numba=A[i];
                if(cnt==(m+n)/2+1)
                {
                    numbb=A[i];
                    return (numba+numbb)/2.0;
                }
                
                i++;
           }
           else
           {
               cnt++;
               if(cnt==(m+n)/2)
                numba=B[j];
                if(cnt==(m+n)/2+1)
                {
                    numbb=B[j];
                    return (numba+numbb)/2.0;
                }
                j++;
           }
         }
         if(i==m)
         {
             while(j<n)
                {
                    cnt++;
                    if(cnt==(m+n)/2)
                        numba=B[j];
                    if(cnt==(m+n)/2+1)
                    {
                        numbb=B[j];
                         return (numba+numbb)/2.0;
                    }
                        
                     j++;  
                }
         }
         if(j==n)
         {
             while(i<m)
                {
                     
                    cnt++;
                    if(cnt==(m+n)/2)
                        numba=A[i];
                    if(cnt==(m+n)/2+1)
                    {
                         numbb=A[i];
                         return (numba+numbb)/2.0;
                    }
                       
                    i++;
                   
                }
             
         }
         mediasum = (numba+numbb)/2.0;
       }
      return mediasum;
    }
};


posted @ 2014-03-18 10:46  海滨银枪小霸王  阅读(112)  评论(0)    收藏  举报