@@ -0,0 +1,1879 @@
+package main.algorithm.leetcode;%0A%0Aimport org.junit.Test;%0A%0A/**%0A * Title : main.algorithm.leetcode %3Cbr%3E%0A * Description :%0A *  %3Cp%3E Runtime: 1 ms, faster than 100.00%25 of Java online submissions for Add Binary.%0A *  %3Cp%3E Memory Usage: 36.4 MB, less than 99.92%25 of Java online submissions for Add Binary.%0A * @author chile%0A * @version 1.0%0A * @date 2019/7/17 13:54%0A */%0Apublic class AddBinary %7B%0A%0A    class Solution %7B%0A        public String addBinary(String a, String b) %7B%0A            if (a.length() %3C b.length()) %7B%0A                String t = a;%0A                a = b;%0A                b = t;%0A            %7D%0A            int la = a.length(), lb = b.length(), carry = 0, num = 0;%0A            char%5B%5D c = new char%5Bla + 1%5D;%0A            for (int i = 0; i %3C c.length; i++) %7B%0A                if (i %3C lb) %7B%0A                    num = (a.charAt(la - i - 1) + b.charAt(lb - i - 1) + carry - 2 * '0') %25 2;%0A                    c%5Bc.length - i - 1%5D = (char) ('0' + num);%0A                    carry = (a.charAt(la - i - 1) + b.charAt(lb - i - 1) + carry - 2 * '0') / 2;%0A                %7D else if (i %3C la) %7B%0A                    num = (a.charAt(la - i - 1) + carry - '0') %25 2;%0A                    c%5Bc.length - i - 1%5D = (char) ('0' + num);%0A                    carry = (a.charAt(la - i - 1) + carry - '0') / 2;%0A                %7D else if (carry %3E 0) %7B%0A                    num = carry;%0A                    c%5Bc.length - i - 1%5D = (char) ('0' + num);%0A                %7D%0A            %7D%0A%0A            return carry %3E 0 ? String.valueOf(c) : String.valueOf(c, 1, la);%0A        %7D%0A    %7D%0A%0A    @Test%0A    public void test1() %7B%0A        assert %22100%22.equals(new Solution().addBinary(%2211%22, %221%22));%0A    %7D%0A%0A    @Test%0A    public void test2() %7B%0A        assert %2210101%22.equals(new Solution().addBinary(%221010%22, %221011%22));%0A    %7D%0A%0A    @Test%0A    public void test3() %7B%0A        assert %22110110%22.equals(new Solution().addBinary(%22100%22, %22110010%22));%0A    %7D%0A%7D%0A

Add Binary