# leetcode #67 in cpp

Solution:

It is the same as #66. We use 2 instead of 10 as the modulo.

Code:

class Solution {
public:
string addBinary(string a, string b) {
int carry = 0;
if(a.length() < b.length()){
string temp = a;
a = b;
b = temp;
}
int alen = a.length();
int blen = b.length();
int i = 0;
while(i<alen){
if(i<blen){
if(a[alen - i - 1] - '0' + b[blen - i - 1]-'0' + carry>=2){
a[alen - i - 1] = (a[alen - i - 1] - '0' + b[blen - i - 1]-'0' + carry) % 2 + '0';
carry = 1;
}
else{
a[alen - i - 1] = (a[alen - i - 1] - '0' + b[blen - i - 1]-'0' + carry) + '0';
carry = 0;
}
}else{
if(a[alen - i - 1] - '0' + carry>=2){
a[alen - i - 1] = (a[alen - i - 1] - '0' + carry) % 2 + '0';
carry = 1;
}else{
a[alen - i - 1] = (a[alen - i - 1] - '0' + carry) + '0';
carry = 0;
break;
}
}
i++;
}
if(carry) a = '1' + a;
return a;
}
};

#### [LeetCode]679. 24 Game

2017-10-19 09:55:53

#### LeetCode Solution(持续更新，java>c++)

2014-04-13 21:52:51

#### leetcode 日经贴，Cpp code -Plus One

2015-05-06 12:51:45

#### leetcode #117 in cpp

2016-06-19 05:41:33

2015-07-05 21:49:19

#### LeetCode 67 : Add Binary (Java)

2015-12-03 21:08:20

2015-08-02 22:41:45

#### Add Binary | leetcode 67 【Java解题报告】

2015-10-04 14:23:39

#### 67. Add Binary Leetcode Python

2015-02-04 23:43:05