Why my code works fine with custom test cases but wrong while formal submission with the same case?


  • 0
    K
    double findMedianSortedArrays(int* nums1, int nums1Size, int* nums2, int nums2Size) {
    if( (!nums1 && !nums2) || !(nums1Size + nums2Size) )return 0;
    int count = (nums1Size + nums2Size);
    int *AllBuf = (int *)malloc( count * sizeof(int) );
    if( !AllBuf )return -1;
    int index = 0, index1 = 0, index2 = 0;
    for( ;index < count; ++index )
    {
        if( nums1[index1] >= nums2[index2] || index2 == nums2Size )
        {
            AllBuf[index] = nums1[index1];
            ++index1;
        }
        else
        {
            AllBuf[index] = nums2[index2];
            ++index2;
        }
    }
    double ret =  (count % 2) ? AllBuf[(count + 1)/2 - 1] : ((double)AllBuf[count/2 - 1] + (double)AllBuf[count/2])/2.0;
    free(AllBuf);
    AllBuf = NULL;
    return ret;
    

    }
    It works fine with custom case([] [2,3]) but got error when submission with the same case([]
    [2,3]), return value = 17259313.00000.
    Looks like a overflow or some memory issues but have no idea where they are. Please help!


Log in to reply
 

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