Monday, May 12, 2014

LeetCode: Add Binary

Given two binary strings, return their sum (also a binary string).

For example,
a = "11"
b = "1"
Return "100".


The calculation for binary is the same thing as natural numbers. Add bit by bit plus carry on digit.

C++ Code:

/*
 * func: add_binary
 * goal: add tow binary numbers in string
 * @param a: binary string a
 * @param b: binary string b
 * return: the new sum in binary string
 */
string add_binary(string a, string b){
    string result = "";
    int iter_a = a.size()-1;
    int iter_b = b.size()-1;
    int carry_on = 0;
    while(iter_a >= 0 && iter_b >= 0){
        int curr_bit = (a[iter_a]-'0') ^ (b[iter_b]-'0') ^ carry_on;
        if(curr_bit == 0){
            //If result is 0 and two bits are 0, then we don't need to carry on
            if(a[iter_a] == '0' && b[iter_b] == '0'){
                carry_on = 0;
            }else{
                //For cases there are two 1s and one 0
                carry_on = 1;
            }
            result = "0" + result;
        }else{
            //For case there are three 1s
            if(a[iter_a] == '1' && b[iter_b] == '1'){
                carry_on = 1;
            }else{
                carry_on = 0;
            }
            result = "1" + result;
        }
        --iter_a;
        --iter_b;
    }
    //Adding remaining bits in a
    while(iter_a >= 0){
        int curr_bit = (a[iter_a]-'0') ^ carry_on;
        if(curr_bit == 0){
            if(a[iter_a] == '0'){
                carry_on = 0;
            }else{
                carry_on = 1;
            }
            result = "0" + result;
        }else{
            carry_on = 0;
            result = "1" + result;
        }
        --iter_a;
    }
    //Adding remaining bits in b
    while(iter_b >= 0){
        int curr_bit = (b[iter_b]-'0') ^ carry_on;
        if(curr_bit == 0){
            if(b[iter_b] == '0'){
                carry_on = 0;
            }else{
                carry_on = 1;
            }
            result = "0" + result;
        }else{
            carry_on = 0;
            result = "1" + result;
        }
        --iter_b;
    }
    if(carry_on){
        result = "1" + result;
    }
    
    return result;
}

Python Code:

# func: add two binary numbers represented by string
# @param a: string a
# @param b: string b
# @return: sum string
def add_binary(a, b):
    result = ''
    carry_on = 0
    iter_a = len(a)-1
    iter_b = len(b)-1
    while iter_a >= 0 or iter_b >= 0 or carry_on:
        if iter_a >= 0:
            a_val = int(a[iter_a])
            iter_a -= 1
        else:
            a_val = 0

        if iter_b >= 0:
            b_val = int(b[iter_b])
            iter_b -= 1
        else:
            b_val = 0

        sum = a_val + b_val + carry_on
        #If sum is bigger than 2, which means there are at least 2 '1' bits, 
        #then we need carry on
        carry_on = sum / 2
        result = str(sum % 2) + result

    return result

No comments:

Post a Comment