-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path67.add-binary.cpp
More file actions
47 lines (46 loc) · 945 Bytes
/
67.add-binary.cpp
File metadata and controls
47 lines (46 loc) · 945 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
44
45
46
47
/*
* @lc app=leetcode id=67 lang=cpp
*
* [67] Add Binary
*/
// @lc code=start
#include <sstream>
#include <string>
using namespace std;
class Solution
{
public:
int c2i(char c)
{
return c - '0';
}
string addBinary(string a, string b)
{
std::stringstream ss;
int i = a.length() - 1;
int j = b.length() - 1;
int t = 0;
while (i >= 0 || j >= 0)
{
int n1 = i >= 0 ? c2i(a[i]) : 0;
int n2 = j >= 0 ? c2i(b[j]) : 0;
int n3 = (n1 + n2 + t) % 2;
t = (n1 + n2 + t) / 2;
ss << (char)(n3 + '0');
i--;
j--;
}
if (t != 0)
{
ss << '1';
}
std::stringstream res;
string tmp = ss.str();
for (int i = tmp.length() - 1; i >= 0; i--)
{
res << tmp[i];
}
return res.str();
}
};
// @lc code=end