-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdd Binary.cpp
More file actions
43 lines (39 loc) · 814 Bytes
/
Copy pathAdd Binary.cpp
File metadata and controls
43 lines (39 loc) · 814 Bytes
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
#include <cstdio>
#include <string>
#include <cstring>
#include <cstdlib>
using namespace std;
class Solution {
public:
string addBinary(string a, string b) {
int lena = a.length();
int lenb = b.length();
int n = max(lena, lenb);
char *tmp = (char*)malloc((n+2)*sizeof(char));
memset(tmp, 0, (n+2)*sizeof(char));
int inc = 0;
for(int i=1; i<=n; ++i){
int opa = i<=lena ? a[lena-i]-'0' : 0;
int opb = i<=lenb ? b[lenb-i]-'0' : 0;
tmp[n+1-i] = (opa+opb+inc)%2 + '0';
inc = (opa+opb+inc) / 2;
}
tmp[0] = inc+'0';
string ans("0");
for(int i=0; i<n+2; ++i){
if(tmp[i] == '1'){
ans = string(tmp+i);
break;
}
}
free(tmp);
return ans;
}
};
int main()
{
Solution s;
printf("%s\n", s.addBinary("11111110", "111111110").c_str());
scanf("%*c");
return 0;
}