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

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

public String addBinary(String a, String b) {
if (a == null || b == null || a.length() == 0 || b.length() == 0) {
return null;
}
int i = a.length() - 1,j = b.length() - 1;
int temp,k = 0;
StringBuilder stringBuilder = new StringBuilder();
//下面和归并排序的Merge部分有点像
while (i >= 0 && j >= 0) {
temp = a.charAt(i--) - '0' + b.charAt(j--) - '0' + k;
if (temp == 0 || temp == 1) {
stringBuilder.append(temp);
k = 0;
} else {
if (temp == 2) {
stringBuilder.append(0);
k = 1;
} else {
stringBuilder.append(1);
k = 1;
}
}
}
while (i >= 0) {
temp = a.charAt(i--) - '0' + k;
if (temp  == 0 || temp == 1) {
stringBuilder.append(temp);
k = 0;
} else {
stringBuilder.append(0);
k = 1;
}
}
while (j >= 0) {
temp = b.charAt(j--) - '0' + k;
if (temp  == 0 || temp == 1) {
stringBuilder.append(temp);
k = 0;
} else {
stringBuilder.append(0);
k = 1;
}
}
//如果最后还有进位
if (k == 1) {
stringBuilder.append(1);
}
return stringBuilder.reverse().toString();//逆序输出
}



class Solution(object):
"""
:type a: str
:type b: str
:rtype: str
"""
i = len(a) - 1
j = len(b) - 1
carry = 0
c = ''
while i >= 0 or j >= 0 or carry > 0:
temp = 0
if i >= 0:
temp += int(a[i])
i -= 1
if j >= 0:
temp += int(b[j])
j -= 1
temp += carry
if temp == 0:
c = '0' + c
carry = 0
elif temp == 1:
c = '1' + c
carry = 0
elif temp == 2:
c = '0' + c
carry = 1
else:
c = '1' + c
carry = 1
return c