Why my program has a Runtime Error "Last executed input: [], [2,3]"?


  • 0
    H

    class Solution {
    public:

    int median(int a[], int n)  {
    	if (a==NULL || n==0) return -1;
    	if (n==1) return a[0];
    
    	if (n%2 == 0) return (a[n/2-1] + a[n/2])/2;
    	else return a[n/2];
    }
    
    //My guess is it means my program does not handle the first array input correctly when it pass NULL through 
    //unknown sized array parameter. Should I return -1 or zero if it is NULL?
    
    double findMedianSortedArrays(int A[], int m, int B[], int n) {
    	if ((!A || m<=0) && (!B || n<=0)) return -1;
    	if (A!=NULL && m==1 && B!=NULL && n==1) return (A[0]+B[0])/2;
    
    	int mA, mB;
    	mA = median(A, m);
    	mB = median(B, n);
    	if (mA==mB) return mA;
    	if (mA < mB)
    		return findMedianSortedArrays(A+m-(m/2+1), m/2+1, B, n/2+1);
    	else
    	return findMedianSortedArrays(A, m/2+1, B+n-(n/2+1), n/2+1);
    }
    

    };


  • 0
    M

    m may be not equal to n . the second round is wrong


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.