MCPcopy Create free account
hub / github.com/Jack-Lee-Hiter/AlgorithmsByPython / addBinary

Method addBinary

leetcode/67. AddBinary.py:9–26  ·  view source on GitHub ↗
(self, a, b)

Source from the content-addressed store, hash-verified

7'''
8class Solution(object):
9 def addBinary(self, a, b):
10 lenA, lenB = len(a), len(b)
11 a, b = list(a), list(b)
12 if lenA == 0:
13 return b
14 if lenB == 0:
15 return a
16 carry, r = 0, ['']*(max(lenA, lenB)+1)
17 for i in range(len(r)):
18 p1, p2 = 0, 0
19 if i < lenA:
20 p1 = int(a[lenA-1-i])
21 if i < lenB:
22 p2 = int(b[lenB-1-i])
23 sum = p1 + p2 + carry
24 r[len(r)-1-i] = str(sum%2)
25 carry = sum // 2
26 return str(int("".join(r)))
27s = Solution()
28print(s.addBinary('10110', '101'))

Callers 1

67. AddBinary.pyFile · 0.80

Calls

no outgoing calls

Tested by

no test coverage detected