@@ -0,0 +1,2080 @@
+import org.junit.Test;%0A%0Aimport static org.junit.Assert.assertEquals;%0A%0A/**%0A * https://leetcode.com/problems/add-binary/%0A */%0Apublic class Solution %7B%0A%0A    public String addBinary(String a, String b) %7B%0A        StringBuilder sb = new StringBuilder();%0A        int i = 1;%0A        char carry = '0';%0A        while (i %3C= a.length() && i %3C= b.length()) %7B%0A            char x = a.charAt(a.length() - i);%0A            char y = b.charAt(b.length() - i);%0A            AdderResult r = adder(x, y, carry);%0A            carry = r.carry;%0A            sb.append(r.sum);%0A            i++;%0A        %7D%0A%0A        if (a.length() != b.length()) %7B%0A            String longer = a.length() %3E b.length() ? a : b;%0A            while (i %3C= longer.length()) %7B%0A                char x = longer.charAt(longer.length() - i);%0A                AdderResult r = adder(x, '0', carry);%0A                carry = r.carry;%0A                sb.append(r.sum);%0A                i++;%0A            %7D%0A        %7D%0A%0A        if (carry == '1') %7B%0A            sb.append('1');%0A        %7D%0A%0A        return sb.reverse().toString();%0A    %7D%0A%0A%0A    private AdderResult adder(char x, char y, char carry) %7B%0A        if (x == y) %7B%0A            if (x == '0') %7B%0A                return new AdderResult(carry, '0');%0A            %7D else %7B%0A                if (carry == '0') %7B%0A                    return new AdderResult('0', '1');%0A                %7D else %7B%0A                    return new AdderResult('1', '1');%0A                %7D%0A            %7D%0A        %7D else %7B%0A            if (carry == '0') %7B%0A                return new AdderResult('1', '0');%0A            %7D else %7B%0A                return new AdderResult('0', '1');%0A            %7D%0A        %7D%0A    %7D%0A%0A    public class AdderResult %7B%0A        char sum;%0A        char carry;%0A%0A        public AdderResult(char sum, char carry) %7B%0A            this.sum = sum;%0A            this.carry = carry;%0A        %7D%0A    %7D%0A%0A    @Test%0A    public void test() %7B%0A        assertEquals(%221000%22, addBinary(%221%22, %22111%22));%0A    %7D%0A%0A    @Test%0A    public void test_2() %7B%0A        assertEquals(%22%22, addBinary(%0A                %221010000010010%22,%0A                %22110101%22));%0A    %7D%0A%7D%0A

Add Binary