-
Notifications
You must be signed in to change notification settings - Fork 613
/
67.py
48 lines (39 loc) · 1.13 KB
/
67.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
'''
Given two binary strings, return their sum (also a binary string).
The input strings are both non-empty and contains only characters 1 or 0.
Example 1:
Input: a = "11", b = "1"
Output: "100"
Example 2:
Input: a = "1010", b = "1011"
Output: "10101"
'''
class Solution(object):
def addBinary(self, a, b):
"""
:type a: str
:type b: str
:rtype: str
"""
result = ""
carry = 0
index_a, index_b = len(a)-1, len(b)-1
while index_a >= 0 and index_b >= 0:
result = (int(a[index_a]) + int(b[index_b]) + carry)%2 + result
carry = (int(a[index_a]) + int(b[index_b]) + carry)%2
index_a -= 1
index_b -= 1
if index_a >= 0:
while index_a >= 0:
result = (int(a[index_a]) + carry)%2 + result
carry = (int(a[index_a]) + carry)%2
index_a -= 1
elif index_b >= 0:
while index_b >= 0:
result = (int(b[index_b]) + carry)%2 + result
carry = (int(b[index_b]) + carry)%2
index_b -= 1
else:
if carry == 1:
result = str(carry) + result
return result