-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAddBinary.h
49 lines (41 loc) · 1016 Bytes
/
AddBinary.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*
Author: naiyong, aonaiyong@gmail.com
Date: Nov 3, 2014
Problem: Add Binary
Difficulty: 2
Source: https://oj.leetcode.com/problems/add-binary/
Notes:
Given two binary strings, return their sum (also a binary string).
For example,
a = "11"
b = "1"
Return "100".
Solution: Math.
*/
#ifndef ADDBINARY_H_
#define ADDBINARY_H_
#include <string>
using std::string;
#include <algorithm>
using std::max;
class Solution {
public:
string addBinary(string a, string b) {
int m = a.size(), n = b.size(), k = max(m, n);
string c(k, ' ');
int carry = 0;
while (m || n) {
int sum = carry;
if (m)
sum += a[--m] - '0';
if (n)
sum += b[--n] - '0';
carry = sum / 2;
c[--k] = sum % 2 + '0';
}
if (carry)
c.insert(c.begin(), '1');
return c;
}
};
#endif /* ADDBINARY_H_ */