LeetCode

题目:

Given two binary strings, return their sum (also a binary string).

For example,
a = "11"
b = "1"
Return "100".

思路:

递归,注意递归终止条件为i < 0 && j < 0 && carray < 1

package manipulation;

public class AddBinary {

    public String addBinary(String a, String b) {
        int alen = a.length();
        int blen = b.length();
        StringBuilder sb = new StringBuilder();
        add(alen - 1, blen - 1, 0, a, b, sb);
        return sb.toString();
    }
    
    private void add(int i, int j, int carry, String a, String b, StringBuilder sb) {
        if (i <0 && j < 0 && carry < 1) return;
        int m = i >= 0 ? (a.charAt(i) - '0') : 0;
        int n = j >= 0 ? (b.charAt(j) - '0') : 0;
        int total = m + n + carry;
        add(i - 1, j - 1, total / 2, a, b, sb);
        sb.append((char)(total % 2 +'0'));
    }
    
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        AddBinary a = new AddBinary();
        System.out.println(a.addBinary("0", "0"));
    }

}
原文地址:https://www.cnblogs.com/null00/p/5087621.html