-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path067AddBinary.py
More file actions
80 lines (68 loc) · 1.48 KB
/
067AddBinary.py
File metadata and controls
80 lines (68 loc) · 1.48 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
"""
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"
"""
"""
Comments
"""
"""
My
"""
class Solution(object):
def addBinary(self, a, b):
"""
:type a: str
:type b: str
:rtype: str
"""
result = []
sm, lg = self.markSmallBig(a, b)
n = 0
i = 0
j = 0
while i < len(sm):
value = sm[i] + lg[i] + n
if value > 1:
n = 1
else:
n = 0
v = value % 2
result.append(v)
i += 1
j += 1
while j < len(lg):
value = lg[j] + n
if value > 1:
n = 1
else:
n = 0
v = value % 2
result.append(v)
j += 1
if n == 1:
result.append(n)
result.reverse()
return ''.join([str(r) for r in result])
def markSmallBig(self, a, b):
a = list(a)
b = list(b)
a.reverse()
b.reverse()
a = [int(p) for p in a]
b = [int(p) for p in b]
if len(a) > len(b):
return b, a
else:
return a, b
"""
Fast
"""
class Solution(object):
def addBinary(self, a, b):
return str(bin(int(a,2) + int(b,2)))[2:]