Share my 2 ms c solution


  • 3

    share my c solution, also you can see my all solutions about leetcode in github :
    https://github.com/lightmen/leetcode.git

    char *addBinary(char *a, char *b) {

    char *ret = NULL;
    int a_len,b_len;
    int carry = 0;
    int i = 0;
    a_len = strlen(a);
    b_len = strlen(b);
    ret = (char *)malloc((a_len+b_len + 2) * sizeof(char));
    i = a_len+b_len;
    ret[i--] = 0;
    
    while(a_len > 0 && b_len > 0){
        --a_len;
        --b_len;
        ret[i--] = '0' + (carry + a[a_len] - '0' + b[b_len] - '0') % 2;
        carry = (carry + a[a_len] - '0' + b[b_len] - '0') / 2;
    }
    while(a_len > 0){
        --a_len;
        ret[i--] = '0' + (carry + a[a_len] - '0') % 2;
        carry = (carry + a[a_len] - '0') / 2;
    }
    while(b_len > 0){
        --b_len;
        ret[i--] = '0' + (carry  + b[b_len] - '0') % 2;
        carry = (carry + b[b_len] - '0') / 2;
    }
    
    if(carry){
        ret[i--] = carry + '0';
    }
    
    return ret+i+1;
    

    }


Log in to reply
 

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