mirror of
https://github.com/arkorty/LeetCode.git
synced 2026-03-17 16:51:46 +00:00
Initial commit
This commit is contained in:
23
Easy/add-binary/solution.cpp
Normal file
23
Easy/add-binary/solution.cpp
Normal file
@@ -0,0 +1,23 @@
|
||||
#include <array>
|
||||
#include <string>
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
std::string addBinary(std::string a, std::string b) {
|
||||
std::string r("");
|
||||
bool c = 0;
|
||||
for (int i = a.size() - 1, j = b.size() - 1; i >= 0 || j >= 0; --i, --j) {
|
||||
bool d = i >= 0 ? a[i] == '1' : 0;
|
||||
bool e = j >= 0 ? b[j] == '1' : 0;
|
||||
|
||||
r = ((bool)(d ^ e ^ c) ? '1' : '0') + r;
|
||||
c = (d & e) || (e & c) || (d & c);
|
||||
}
|
||||
|
||||
if (c) {
|
||||
r = (c ? '1' : '0') + r;
|
||||
}
|
||||
|
||||
return r;
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user