leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

0067.cpp (444B)


      1 class Solution {
      2   public:
      3     string addBinary(string a, string b) {
      4         string res;
      5         int i = a.size() - 1;
      6         int j = b.size() - 1;
      7         int add = 0;
      8         while (i >= 0 || j >= 0 || add) {
      9             if (i >= 0) add += a[i--] - '0';
     10             if (j >= 0) add += b[j--] - '0';
     11             res += to_string(add % 2);
     12             add /= 2;
     13         }
     14         reverse(res.begin(), res.end());
     15         return res;
     16     }
     17 };