67. Add Binary
class Solution(object):
def addBinary(self, a, b):
"""
:type a: str
:type b: str
:rtype: str
"""
if len(a) < len(b):
return self.addBinary(b, a)
b = '0' * (len(a) - len(b)) + b
temp = 0
res = ''
for i in range(len(a)-1, -1, -1):
ans = int(a[i]) + int(b[i]) + temp
if ans == 0:
res = '0' + res
temp = 0
elif ans == 1:
res = '1' + res
temp = 0
elif ans == 2:
res = '0' + res
temp = 1
else:
res = '1' + res
temp = 1
if temp == 1:
res = '1' + res
return res