AC clean Java solution


  • 3
    public String addBinary(String s1, String s2) {
        int i = s1.length() - 1, j = s2.length() - 1, c = 0;
        String s = "";
        
        while (i >= 0 || j >= 0 || c == 1) {
            int a = (i < 0) ? 0 : s1.charAt(i--) - '0';
            int b = (j < 0) ? 0 : s2.charAt(j--) - '0';
            
            s = (char)('0' + a ^ b ^ c) + s;
            c = (a + b + c) >> 1;
        }
        
        return s;
    }

Log in to reply
 

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