这道题也很简单,我开始质疑我刷leetcode是不是不如再把acm捡起来。
class Solution:
def addBinary(self, a: str, b: str) -> str:
count = 0
l1 = len(a)
l2 = len(b)
l = min(l1, l2)
c = ""
i = l1-1
j = l2-1
while i >= 0 and j >= 0:
anow = int(a[i])
bnow = int(b[j])
c = c+str(anow^bnow^count)
count = (anow&bnow)|(anow&count)|(bnow&count)
i -= 1
j -= 1
while i >= 0:
anow = int(a[i])
c = c+str(anow^count)
count = anow&count
i -= 1
while j >= 0:
bnow = int(b[j])
c = c+str(bnow^count)
count = bnow&count
j -= 1
if count:
c = c+str(count)
return c[::-1]