67. 二進制求和
給你兩個二進制字元串,傳回它們的和(用二進制表示)。
輸入為 非空 字元串且隻包含數字 1 和 0。

class Solution {
public:
string addBinary(string a, string b) {
int i = a.size() - 1, j = b.size() - 1;
int add = 0;
string res;
while (i >= 0 && j >= 0) {
int t = a[i--] - '0' + b[j--] - '0' + add;
res.push_back(t % 2 + '0');
add = t / 2;
}
while (i >= 0) {
int t = a[i--] - '0' + add;
res.push_back(t % 2 + '0');
add = t / 2;
}
while (j >= 0) {
int t = b[j--] - '0' + add;
res.push_back(t % 2 + '0');
add = t / 2;
}
if (add) res.push_back('1');
reverse(res.begin(), res.end());
return res;
}
};