/
Add_Binary.cpp
57 lines (56 loc) · 1.33 KB
/
Add_Binary.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//File Name :Add_Binary.cpp
//Author : Avanty
//Date : 2014/5/28 9:53AM
//Description:
//Add Binary Total Accepted: 10037 Total Submissions: 39716 My Submissions
//Given two binary strings, return their sum (also a binary string).
//
//For example,
//a = "11"
//b = "1"
//Return "100".
#include<iostream>
#include<algorithm>
using namespace std;
class Solution {
public:
string addBinary(string a, string b) {
string ret;
int i = a.size()-1;
int j = b.size()-1;
int tmp;
char bit;
int k = 0;
while(i >=0 && j >=0){
bit = (a[i] - '0' + b[j] - '0' + k)%2 + '0';
ret.push_back(bit);
k = (a[i] - '0' + b[j] - '0' + k)/2;
i--;
j--;
cout<<bit<<endl;
}
while(i >= 0){
bit = (a[i] - '0' + k)%2 +'0';
ret.push_back(bit);
k = (a[i] - '0' + k)/2;
i--;
}
while(j >= 0){
bit = (b[j] - '0' + k)%2 +'0';
ret.push_back(bit);
k = (b[j] - '0' + k)/2;
j--;
cout<<bit<<endl;
}
if(k == 1) ret.push_back('1');
reverse(ret.begin(),ret.end());
return ret;
}
};
int main(){
string a = "1";
string b = "111";
Solution sl;
cout<<sl.addBinary(a,b)<<endl;
return 0;
}