forked from ChienJuiLin/leetcode_heprecsler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleetCode_7.py
More file actions
49 lines (40 loc) · 890 Bytes
/
leetCode_7.py
File metadata and controls
49 lines (40 loc) · 890 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
38
39
40
41
42
43
44
45
46
47
48
49
'''
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"
'''
class Solution:
def addBinary(self, a, b):
"""
:type a: str
:type b: str
:rtype: str
"""
a2 = int(a)
b2 = int(b)
ia,ib,p,ans = 0,0,1,0
while a2 > 0:
ia = ia+(a2%10)*2*p
p = p*2
a2 = a2//10
p = 1
while b2 > 0:
ib = ib+(b2%10)*2*p
p = p*2
b2 = b2//10
p = 1
c = ia + ib
while c > 0:
ans = ans + (c%2)*p
c = c//2
p = p*10
return str(ans)
# 這超賤
'''
return format(int(a,2) + int(b,2),'b')
'''