Does the OJ system exists some compiling error


  • 0
    G

    Input: [], [2,3]

    Output: 2.00000

    Expected: 2.50000

    The test sample is tested on VS, which can output the right answer 2.5, however, in the OJ compiling environment, which output 2.

    The test sample is so simple, and go through the code step by step, the answer can be achieved, that is 2.5, why the actual Output is 2.0??

    class Solution 
    {
        public:
    	       double findMedianSortedArrays(int A[], int m, int B[], int n);
    };
    
    double Solution::findMedianSortedArrays(int A[], int m, int B[], int n)
    {
        static int count =0;
        static int mIndex = (m+n-1)/2;
        static bool odd = (m+n)%2;
    
    if(m==0)
    	if(odd)
    		return B[mIndex-count];
    	else
    	{
    	    double res = 0.0;
    	    res += B[mIndex-count];
    	    res += B[mIndex-count+1];
    	    double t = 2.0;
    	    return res/t;
    	}
    
    if(!n)
    	if(odd)
    		return A[mIndex-count];
    	else
    	{
    	    double res = A[mIndex-count]+A[mIndex-count+1];
    	    return res/2;
    	}
    
    int mIndexA = (m-1)/2;
    int mIndexB = (n-1)/2;
    int medianA = A[mIndexA];
    int medianB = B[mIndexB];
    
    if(count == mIndex)
    {
    	if(odd)
    		return A[0]<B[0]?A[0]:B[0];
    	else
    	{
    		int i = 0, j = 0, c=0;
    		double res = 0;
    		while(c!=2)
    		{
    			c++;
    			if(i==m)
    			{
    				res+=B[j];
    				j++;
    				continue;
    			}
    			if(j==n)
    			{
    				res+=A[i];
    				i++;
    				continue;
    			}
    			if(A[i]<=B[j])
    			{
    				res+=A[i];
    				i++;
    			}
    			else
    			{
    				res+=B[j];
    				j++;
    			}
    		}
    		return res/2;
    	}
    }
    
    if(medianB >= medianA)
    {
    	count += mIndexA + 1;
    	return findMedianSortedArrays(A+mIndexA+1,m-mIndexA-1,B,mIndexB+1);
    }
    else
    {
    	count += mIndexB + 1;
    	return findMedianSortedArrays(A,mIndexA+1,B+mIndexB+1,n-mIndexB-1);
    }
    

    }


Log in to reply
 

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