My Java Solution


  • 2
    L
    public String addBinary(String a, String b) {
        int carry = 0, i = a.length()-1, j = b.length()-1, x , y;
        String sum = "";
        while (i >=0 || j >=0 || carry != 0) {
            x = i < 0 ? 0 : a.charAt(i--) - 48;
            y = j < 0 ? 0 : b.charAt(j--) - 48;
            sum = (x ^ y ^ carry) + sum;
            carry = (x + y + carry) / 2; 
        }
        return sum;
    }

Log in to reply
 

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