leetcode67 C++ 4ms 二进制求和 addBinary

class Solution {
public:
    string addBinary(string a, string b) {
        string res = "";
        int c = 0;
        int i = a.size() - 1;
        int j = b.size() - 1;
        while(i >= 0 || j>=0 || c==1){
            c += (i>=0 ? a[i--] - '0':0);
            c += (j>=0 ? b[j--] - '0':0);
            res = char(c%2 + '0') + res;
            c /= 2;
        }
        return res;
    }
};
posted @ 2018-07-26 16:27  一条图图犬  阅读(610)  评论(1编辑  收藏  举报