-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd-binary.h
35 lines (26 loc) · 826 Bytes
/
add-binary.h
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
#ifndef ADD_BINARY_H_
#define ADD_BINARY_H_
#include <string>
namespace solution {
std::string addBinary(std::string a, std::string b) {
// Brute force
// Runtime: 4 ms, faster than 63.38% of C++ online submissions for Add Binary.
// Memory Usage: 6.1 MB, less than 87.97% of C++ online submissions for Add Binary.
//
int offset = 0, l = std::max(a.size(), b.size());
int c1, c2, sum = 0, carry = 0;
std::string result;
while (offset != l) {
c1 = offset > a.size() - 1 ? 0 : a[a.size() - offset - 1] - '0';
c2 = offset > b.size() - 1 ? 0 : b[b.size() - offset - 1] - '0';
sum = c1 + c2 + carry;
carry = sum > 1;
sum %= 2;
result.insert(result.begin(), sum + '0');
offset++;
}
if (carry) result.insert(result.begin(), '1');
return result;
}
}
#endif // ADD_BINARY_H_