Right answer with g++ but wrong answer with OJ


  • 0
    T

    I tired g++ and get the correct answer 1.5 of the case [1,2], [1,2], but OJ says my code gets the wrong answer of 1. Can anyone help?

    double findMedianSortedArrays(int A[], int m, int B[], int n) {
            if(m == 0){
                if(n%2 == 1)
                    return B[n/2];
                else
                    return (double)(B[n/2-1]+B[n/2])/2;
            }
            if(n == 0){
                if(m%2 == 1)
                    return A[m/2];
                else
                    return (double)(A[m/2-1]+A[m/2])/2;
            }
            bool flag = 0;
            int a_id = 0;
            int b_id = 0;
            int cnt = 0;
            int temp = 0;
            for(cnt=0; cnt<=(m+n)/2,a_id<m, b_id<n; cnt++){
                if(A[a_id] >= B[b_id]){
                    b_id ++;
                    flag = 1;
                }else{
                    a_id ++;
                    flag = 0;
                }
            }
            if((m+n)%2 == 1){
                if(a_id == m && cnt <=(m+n)/2)
                    return B[(n-m)/2];
                else if(b_id == n&& cnt <=(m+n)/2){
                    return A[(m-n)/2];
                }
                else if(flag == 0){
                    return A[a_id-1];
                }else{
                    return B[b_id-1];
                }
            }else{
                if(a_id == m && cnt <=(m+n)/2){
                    if(m == n)
                        return (double)(A[m-1]+B[0])/2;
                    else
                        return (double)(B[(n-m)/2-1]+B[(n-m)/2])/2;
                }else if(b_id == n&& cnt <=(m+n)/2){
                    if(m == n)
                        return (double)(A[0]+B[n-1])/2;
                    else
                        return (double)(A[(m-n)/2-1]+A[(m-n)/2])/2;
                }else if(flag == 0){
                    temp = (A[a_id]<B[b_id])?A[a_id]:B[b_id];
                    return (double)(A[a_id-1]+temp)/2;
                }
                else{
                    temp = (A[a_id]<B[b_id])?A[a_id]:B[b_id];
                    return (double)(temp+B[b_id-1])/2;
                }
            }
        }

  • 0
    C

    you can try replacing '2' with '2.0'


  • 0
    T

    I tried changing it but it does not work : (


Log in to reply
 

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