Very short C++ solution


  • 0
    G
        string addBinary(string a, string b) {
            int c = 0, i = a.size() - 1, j = b.size() - 1;
            string r;
            while (i >= 0 || j >= 0 || c > 0) {
                int s = (i >= 0 && a[i--] == '1' ? 1 : 0) + (j >= 0 && b[j--] == '1' ? 1 : 0) + c;
                r = to_string(s % 2) + r;
                c = s / 2;
            }        
            return r;
        }
    

Log in to reply
 

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