1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
| class Solution:
def addBinary(self, A, B):
res = []
carry = 0
while A or B or carry:
carry += (A or [0]).pop() + (B or [0]).pop()
res.append(carry & 1)
carry = carry >> 1
return res[::-1]
def addNegabinary(self, A, B):
res = []
carry = 0
while A or B or carry:
carry += (A or [0]).pop() + (B or [0]).pop()
res.append(carry & 1)
carry = -(carry >> 1)
while len(res) > 1 and res[-1] == 0:
res.pop()
return res[::-1]
|