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
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


你可能感兴趣的:(algorithm)