-
Notifications
You must be signed in to change notification settings - Fork 29
/
0067-Add-Binary.py
59 lines (45 loc) · 1.33 KB
/
0067-Add-Binary.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
49
50
51
52
53
54
55
56
57
58
59
'''
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"
'''
# Method 1
class Solution:
def addBinary(self, a: str, b: str) -> str:
i, j, carry = len(a) - 1, len(b) - 1, '0'
res = []
while(i >= 0 or j >= 0 or carry != '0'):
num1 = a[i] if i >= 0 else '0'
num2 = b[j] if j >= 0 else '0'
if num1 == num2:
res.append(carry)
carry = num1
else:
res.append('1' if carry == '0' else '0')
i -= 1
j -= 1
return ''.join(res[::-1])
# Method 2
class Solution:
def addBinary(self, a: str, b: str) -> str:
i, j, carry, result = len(a) - 1, len(b) - 1, 0, []
while i >= 0 or j >= 0 or carry:
total = carry
if i >= 0:
total += int(a[i])
i -= 1
if j >= 0:
total += int(b[j])
j -= 1
result.append(str(total%2))
carry = total//2
return ''.join(result[::-1])
# Check Custom Input
s = Solution()
answer = s.addBinary('101010', '1111111')
print(answer)