c solution passed specific test cast but got Runtime Error when submitted.


  • 0
    X

    When I submit my code I got Runtime Error with this "101010010000110110111100101111000111111011001110111001111000" "10001110100001111000001110001000000101011011100001011101101001000000101001111010111001110000" case. But when I copy this case into custom test case it comes out correct answer.
    I've been confused with this.
    Here is my code.

    char* addBinary(char* a, char* b) {
        int len_a = strlen(a);
        int len_b = strlen(b);
        int len = (len_a > len_b ? len_a : len_b) + 1;
        int flag = 0, s = 0;
        int i, j, k;
        char *res = (char*)malloc(sizeof(char) * (1 + len));
        res[len + 1] = '\0';
        for(i = 0; i <= len; i++) res[i] = '0'; 
        for(i = len_a - 1, j = len_b - 1, k = len;
            i >= 0 && j >= 0;
            i--, j--, k--
        ){
            s = a[i] - '0' + b[j] - '0' + flag;
            flag = (s & 0x2) >> 1;
            s &= 0x1;
            res[k] += s;
        }
        if(i == j){
            res[k] += flag;
            return res + k + (!flag ? 1 : 0);
        }
        if(i < j){
            while(j >= 0){
                s = b[j--] - '0' + flag;
                flag = (s & 0x2) >> 1;
                s &= 0x1;
                res[k--] += s;
            }
        }else{
            while(i >= 0){
                s = a[i--] - '0' + flag;
                flag = (s & 0x2) >> 1;
                s &= 0x1;
                res[k--] += s;
            }
        }
        res[k] += flag;
        printf("k = %d\n", k);
        printf("res = %s\n", res);
        return res + k + (!flag ? 1 : 0);
    }
    

Log in to reply
 

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