给定两个二进制字符串,返回他们的和(用二进制表示)。
输入为非空字符串且只包含数字 1
和 0
。
示例 1:
输入: a = "11", b = "1"
输出: "100"
示例 2:
输入: a = "1010", b = "1011"
输出: "10101"
class Solution {
public String addBinary(String a, String b) {
StringBuilder sb = new StringBuilder(); int i = a.length()-1;
int j = b.length()-1;
int remainder = 0;
while (i>=0 || j>=0) {
int sum = remainder;
if (i>=0) sum+=a.charAt(i)-'0';
if (j>=0) sum+=b.charAt(j)-'0';
sb.append(sum % 2);
remainder = sum/2;
i--;
j--;
}
if (remainder != 0) {
sb.append(remainder);
} return sb.reverse().toString();
}
}