We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 3fd35d6 commit c73ca5aCopy full SHA for c73ca5a
July-LeetCoding-Challenge/19-Add-Binary/Add-Binary.cpp
@@ -0,0 +1,15 @@
1
+class Solution {
2
+public:
3
+ string addBinary(string a, string b) {
4
+ string ans = (a.length() > b.length())?a:b;
5
+ int sum = 0;
6
+ for (int i = 0; i < ans.length(); i++){
7
+ if (i < a.length()) sum += (a[a.length() - i - 1] == '1')?1:0;
8
+ if (i < b.length()) sum += (b[b.length() - i - 1] == '1')?1:0;
9
+ ans[ans.length() - i - 1] = (sum & 1) + '0';
10
+ sum >>= 1;
11
+ }
12
+ if (sum == 1) ans = "1" + ans;
13
+ return ans;
14
15
+};
0 commit comments