Skip to content

Commit 3414a2d

Browse files
authored
Create adding-two-negabinary-numbers.py
1 parent 9714df6 commit 3414a2d

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Time: O(n)
2+
# Space: O(n)
3+
4+
class Solution(object):
5+
def addNegabinary(self, arr1, arr2):
6+
"""
7+
:type arr1: List[int]
8+
:type arr2: List[int]
9+
:rtype: List[int]
10+
"""
11+
result = []
12+
carry = 0
13+
while arr1 or arr2 or carry:
14+
if arr1:
15+
carry += arr1.pop()
16+
if arr2:
17+
carry += arr2.pop()
18+
result.append(carry & 1)
19+
carry = -(carry >> 1)
20+
while len(result) > 1 and result[-1] == 0:
21+
result.pop()
22+
result.reverse()
23+
return result

0 commit comments

Comments
 (0)