-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ67
More file actions
45 lines (44 loc) · 1.07 KB
/
Q67
File metadata and controls
45 lines (44 loc) · 1.07 KB
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
class Solution:#1
def addBinary(self, a, b):
"""
:type a: str
:type b: str
:rtype: str
"""
grow=0
answer=''
lena,lenb=len(a),len(b)
if lena<lenb:
b,a=a,b
lena,lenb=lenb,lena
for i in range(lenb):
num=int(a[-1-i])+int(b[-1-i])+grow
if num == 2:
num,grow=0,1
elif num==3:
num,grow=1,1
else:
grow=0
answer=answer+str(num)
for i in range(lenb,lena):
num=int(a[-1-i])+grow
if num == 2:
num,grow=0,1
elif num==3:
num,grow=1,1
else:
grow=0
answer=answer+str(num)
if grow==1:
answer=answer+str(1)
return answer[::-1]
class Solution:#solution
def addBinary(self, a, b):
"""
:type a: str
:type b: str
:rtype: str
"""
a = int(a,2);
b = int(b,2);
return str(bin(a+b))[2:];