8000
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 4699f2e commit a37f9cfCopy full SHA for a37f9cf
Add_Binary.py
@@ -0,0 +1,31 @@
1
+class Solution:
2
+ # @param {string} a a number
3
+ # @param {string} b a number
4
+ # @return {string} the result
5
+ def addBinary(self, a, b):
6
+ if len(a) < len(b): a, b = b, a
7
+ pa = len(a) - 1
8
+ pb = len(b) - 1
9
+ carry = 0
10
+ result = ''
11
+
12
+ while pb >= 0:
13
+ sum = int(a[pa]) + int(b[pb]) + carry
14
+ result = str(sum%2) + result
15
+ carry = sum / 2
16
+ pb -= 1
17
+ pa -= 1
18
19
+ while pa >= 0:
20
+ sum = int(a[pa]) + carry
21
22
23
24
< 5C2D /code>
25
+ if carry == 1: result = '1' + result
26
+ return result
27
28
+if __name__ == '__main__':
29
+ a = Solution()
30
+ res = a.addBinary('110101','10010001011')
31
+ print res
0 commit comments