-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAddBinary.py
34 lines (31 loc) · 986 Bytes
/
AddBinary.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
#This is the link from Leetcode: https://leetcode.com/problems/add-binary/
#Summary of the problem: Given 2 strings, each string contains '0' or '1'. In another word, they are binary string.
#Question: How can we sum two of these strings
#Example:
#Input: a = "11", b = "1"
#Output: "100"
#My solution:
class Solution:
def addBinary(self, a: str, b: str) -> str:
res = ''
x = int(a) + int(b)
cnt = 0
x = str(x)
for i in range (len(x) - 1, -1,-1):
if ( int(x[i]) + cnt ) == 2 :
cnt = 1
res += '0'
elif ( int(x[i]) + cnt ) == 1:
res += '1'
cnt = 0
elif ( int(x[i]) + cnt ) == 3:
res += '1'
cnt = 1
else:
res += '0'
cnt = 0
if cnt == 1:
res += '1'
return res[::-1]
#Performance of the solution: Beat 95.4% time
# This is a test