LeetCode蹂躏集

LeetCode 67. Add Binary

2018-01-26  本文已影响0人  alexsssu

使用数组模拟两个二进制字符串加法,使用字符c标记当前位字符。

class Solution {
public:
    string addBinary(string a, string b) {
        string s;
        for (int c = 0, i = a.size()-1, j = b.size()-1; i>=0 || j>=0 || c == 1; --i,--j) {
            c += i >= 0 ? a[i] - '0' : 0;
             c += j >= 0 ? b[j] - '0' : 0;
           s = char(c % 2 + '0') + s;
           c /= 2;
        }
        return s;
        }
};
上一篇 下一篇

猜你喜欢

热点阅读