【leetcode】67. Add Binary 字符串形式的二进制的加法

1. 题目

Given two binary strings, return their sum (also a binary string).

For example,
a = “11”
b = “1”
Return “100”.

2. 思路

从后往前相加和进位。

3. 代码

class Solution {
public:
    string addBinary(string a, string b) {
        string ret;
        int la = a.length();
        int lb = b.length();
        if (la == 0) { return b; }
        if (lb == 0) { return a; }
        int lmax = max(la, lb);
        int add = '0';
        for (int i = 0; i < lmax; i++) {
            char ca = la > i ? a[la-i-1] : '0';
            char cb = lb > i ? b[lb-i-1] : '0';
            char s = (ca == cb ? '0' : '1');
            char sj = (s == add ? '0' : '1');
            if (ca == '1' && cb == '1'
                    || s == '1' && add == '1') {
                add = '1';        
            } else {
                add = '0';
            }
            ret += sj;
        }
        if (add == '1') {
            ret += add;
        }
        reverse(ret.begin(), ret.end());
        return ret;
    }
};
    原文作者:knzeus
    原文地址: https://segmentfault.com/a/1190000007383065
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞