forked from daiwb/Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddBinary.cpp
More file actions
37 lines (34 loc) · 881 Bytes
/
addBinary.cpp
File metadata and controls
37 lines (34 loc) · 881 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
#include <iostream>
#include <vector>
#include <deque>
#include <algorithm>
using namespace std;
#define REP(i,n) for(int i=0;i<(n);++i)
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define RFOR(i,a,b) for(int i=(a);i>=(b);--i)
class Solution {
public:
string addBinary(string a, string b) {
if (a.length() > b.length()) swap(a, b);
int la = a.length(), lb = b.length();
int add = 0;
REP(i,la) {
int s = a[la - 1 - i] - '0' + b[lb - 1 - i] - '0' + add;
add = s / 2;
s %= 2;
b[lb - 1 - i] = s + '0';
}
FOR(i,la,lb-1) {
int s = b[lb - 1 - i] - '0' + add;
add = s / 2;
s %= 2;
b[lb - 1 - i] = s + '0';
if (add == 0) break;
}
if (add == 1) b = "1" + b;
return b;
}
};
int main() {
return 0;
}