-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetcode - Add Binary.py
More file actions
143 lines (134 loc) · 5.57 KB
/
Leetcode - Add Binary.py
File metadata and controls
143 lines (134 loc) · 5.57 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
### Given two binary strings a and b, return their sum as a binary string.
### Example 1:
### Input: a = "11", b = "1"
### Output: "100"
### Example 2:
### Input: a = "1010", b = "1011"
### Output: "10101"
### Constraints:
### 1 <= a.length, b.length <= 104
### a and b consist only of '0' or '1' characters.
### Each string does not contain leading zeros except for the zero itself.
class Solution:
def addBinary(self, a: str, b: str) -> str:
stack = []
carry = 0
len_a = len(a)
len_b = len(b)
if len_a>=len_b:
for x in range(len_a):
if x>=len_b:
if int(a[len_a-x-1]) == 1 and carry == 1:
stack.append("0")
carry = 1
continue
elif int(a[len_a-x-1]) == 1 and carry == 0:
stack.append("1")
carry = 0
continue
elif int(a[len_a-x-1]) == 0 and carry == 1:
stack.append("1")
carry = 0
print(stack)
continue
else:
if carry == 0:
stack.append("0")
else:
stack.append("1")
carry = 0
else:
if int(a[len_a-x-1]) == 1 and int(b[len_b-x-1]) == 1 and carry == 0:
carry = 1
stack.append("0")
continue
elif int(a[len_a-x-1]) == 0 and int(b[len_b-x-1]) == 1 and carry == 0:
carry = 0
stack.append("1")
continue
elif int(a[len_a-x-1]) == 1 and int(b[len_b-x-1]) == 0 and carry == 0:
carry = 0
stack.append("1")
continue
elif int(a[len_a-x-1]) == 1 and int(b[len_b-x-1]) == 1 and carry == 1:
carry = 1
stack.append("1")
continue
elif int(a[len_a-x-1]) == 0 and int(b[len_b-x-1]) == 1 and carry == 1:
carry = 1
stack.append("0")
continue
elif int(a[len_a-x-1]) == 1 and int(b[len_b-x-1]) == 0 and carry == 1:
carry = 1
stack.append("0")
continue
elif int(a[len_a-x-1]) == 0 and int(b[len_b-x-1]) == 0 and carry == 1:
carry = 0
stack.append("1")
continue
else:
carry = 0
stack.append("0")
continue
if carry == 1:
stack.append(str(carry))
else:
for x in range(len_b):
if x>=len_a:
if carry == 1 and int(b[len_b-x-1]) == 1:
stack.append("0")
carry = 1
continue
elif carry == 0 and int(b[len_b-x-1]) == 1:
stack.append("1")
carry = 0
continue
elif carry == 1 and int(b[len_b-x-1]) == 0:
stack.append("1")
carry = 0
continue
else:
if carry == 0:
stack.append("0")
else:
stack.append("1")
carry = 0
else:
if int(b[len_b-x-1]) == 1 and int(a[len_a-x-1]) == 1 and carry == 0:
carry = 1
stack.append("0")
continue
elif int(b[len_b-x-1]) == 0 and int(a[len_a-x-1]) == 1 and carry == 0:
carry = 0
stack.append("1")
continue
elif int(b[len_b-x-1]) == 1 and int(a[len_a-x-1]) == 0 and carry == 0:
carry = 0
stack.append("1")
continue
elif int(b[len_b-x-1]) == 1 and int(a[len_a-x-1]) == 1 and carry == 1:
carry = 1
stack.append("1")
continue
elif int(b[len_b-x-1]) == 0 and int(a[len_a-x-1]) == 1 and carry == 1:
carry = 1
stack.append("0")
continue
elif int(b[len_b-x-1]) == 1 and int(a[len_a-x-1]) == 0 and carry == 1:
carry = 1
stack.append("0")
continue
elif int(b[len_b-x-1]) == 0 and int(a[len_a-x-1]) == 0 and carry == 1:
carry = 0
stack.append("1")
continue
else:
carry = 0
stack.append("0")
continue
if carry == 1:
stack.append(str(carry))
stack.reverse()
return "".join(stack)
# Runtime: 41 ms, faster than 67.15% of Python3 online submissions for Add Binary.
# Memory Usage: 14.3 MB, less than 24.35% of Python3 online submissions for Add Binary.