-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path67AddBinary.py
More file actions
37 lines (37 loc) · 914 Bytes
/
Copy path67AddBinary.py
File metadata and controls
37 lines (37 loc) · 914 Bytes
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
class Solution:
def addBinary(self, a, b):
"""
:type a: str
:type b: str
:rtype: str
"""
a=a[::-1]
b=b[::-1]
c=''
flag=0
if a and b:
while a and b:
c+=str((int(a[0])+int(b[0])+flag)%2)
flag=(int(a[0])+int(b[0])+flag)//2
a=a[1:]
b=b[1:]
if a:
while a:
c+=str((int(a[0])+flag)%2)
flag=(int(a[0])+flag)//2
a=a[1:]
if b:
while b:
c+=str((int(b[0])+flag)%2)
flag=(int(b[0])+flag)//2
b=b[1:]
if flag==1:
c+='1'
c=c[::-1]
return c
if __name__=='__main__':
a='11'
b='1'
solution=Solution()
result=solution.addBinary(a,b)
print(result)