class Solution {
public:
    string addBinary(string& a, string& b) {
        string res = "";
        int m = a.size() - 1, n = b.size() - 1, carry = 0;
        while (m >= 0 || n >= 0) {
            int p = m >= 0 ? a[m--] - '0' : 0;
            int q = n >= 0 ? b[n--] - '0' : 0;
            int sum = p + q + carry;
            res = to_string(sum % 2) + res;
            carry = sum / 2;
        }
        return carry == 1 ? "1" + res : res;
    }
};

相关文章:

  • 2021-07-28
  • 2021-05-03
  • 2021-10-11
  • 2021-09-26
  • 2021-09-08
  • 2021-07-28
猜你喜欢
  • 2021-06-28
相关资源
相似解决方案