题目
给你两个二进制字符串,返回他们的和(用二进制表示)。
代码
```def addBinary(a,b):
if len(a) < len(b):
a,b = b,a
a = a[::-1]
b = b[::-1]
b += (len(a)-len(b))*'0'
cin = 0
c = []
for i in range(len(a)):
aList = [a[i],b[i],cin]
if aList.count('1') == 1 or aList.count('1') == 3:
c.append('1')
else:
c.append('0')
if aList.count('1') > 1:
cin = '1'
else:
cin = '0'
if cin == '1':
c.append('1')
return ''.join(c[::-1])
print(addBinary('11', '1101'))
网友评论