C++ simple solution.


  • 7
    C
    string addBinary(string a, string b) {
        int i = a.size()-1, j = b.size()-1;
        int carry = 0;
        string ret = "";
        while (i >= 0 || j >= 0 || carry!=0) {
            if (i >= 0) {
                carry += a[i]=='0'?0:1;
                i--;
            }
            if (j >= 0) {
                carry += b[j]=='0'?0:1;
                j--;
            }
            ret = ((carry%2)==0?"0":"1") + ret;
            carry /= 2;
        }
        return ret;
    }

Log in to reply
 

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