leetcode67 二进制求和
题目链接:https://leetcode-cn.com/problems/add-binary/submissions/
反思:1.用reverse()函数将string倒置后操作,不用进位时整体向后移动
2.t的巧妙用法
3.to_string函数的使用和理解
class Solution {
public:
string addBinary(string a, string b) {
reverse(a.begin(), a.end());
reverse(b.begin(), b.end());
string c;
for (int i = 0, t = 0; i < a.size() || i < b.size() || t; i ++ ) {
if (i < a.size()) t += a[i] - '0';
if (i < b.size()) t += b[i] - '0';
c += to_string(t % 2);
t /= 2;
}
reverse(c.begin(), c.end());
return c;
}
};