Calculator tell me it is right but oj tell me wrong?


  • 0
    F
    char* addBinary(char* a,char* b){
        long aLength = strlen(a);
        long bLength = strlen(b);
        long maxLength = aLength > bLength ? aLength : bLength;
        long i,j,k = 0,tmp = 0;
        long tmpa,tmpb;
        char* result = (char*) malloc(sizeof(maxLength + 2));
        for (i = aLength - 1,j = bLength - 1 ; i > -1 || j > -1; i--,j--){
            if( i<=-1){
                tmpa = 0;
            } else{
                tmpa = a[i] - 48 ;
            }
            if( j <=-1){
                tmpb = 0;
            } else{
                tmpb = b[j] - 48 ;
           }
           tmp =tmpa + tmpb + tmp ;
           if(tmp > 1){
                result[k++] = tmp - 2 + '0';
                tmp = 1;
           } else{
                result[k++] = tmp + '0';
                tmp = 0;
           }
        }
        if(tmp == 1){
            result[k] = '1';
        } else{
            k--;
        }
        for(i = 0;i <= k / 2;i++){
            tmp = result[i];
            result[i] = result[k - i];
            result[k-i] = tmp;
        }
        result[k + 1] = '\0';
        return result;
    }
    

    oj tell me runtime error
    the case is:
    "10100000100100110110010000010101111011011001101110111111111101000000101111001110001111100001101"
    "110101001011101110001111100110001010100001101011101010000011011011001011101111001100000011011110011"

    my result is :
    "110111101100010011000101110110100000011101000101011001000011011000001100011110011010010011000000000"

    who can tell me the reason why it is wrong?


  • 0
    F

    wtf

    char* result = (char*) malloc(sizeof(maxLength + 2)); -->
    char* result = (char*) malloc(maxLength + 2);

    fix it


Log in to reply
 

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