-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path67_Add_Binary.py
More file actions
58 lines (48 loc) · 1.39 KB
/
67_Add_Binary.py
File metadata and controls
58 lines (48 loc) · 1.39 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
"""
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.
Input: a = "11", b = "1"
Output: "100"
Input: a = "1010", b = "1011"
Output: "10101"
两个二进制的数字相加,返回二进制的结果
从二进制数的右边往左边看,每一位都要乘以 2的x次方,
x= len(s)-i,是第i为,写一个转换函数即可
十进制转二进制,除以二,余数就是二进制的位,然后反着输出即可
"""
class Solution(object):
def addBinary(self, a, b):
"""
:type a: str
:type b: str
:rtype: str
"""
so = Solution()
adcm = so.binaryToDecimal(a)
bdcm = so.binaryToDecimal(b)
res = so.decimalToBinary(adcm+bdcm)
print(adcm,bdcm,res)
return res
#1011 l = 4
def binaryToDecimal(self,s):
l = len(s)
sum = 0
for i in range(l-1,-1,-1):
sum += int(s[i])*(2**(l-1-i))
return sum
def decimalToBinary(self,num):
rs = []
if num == 0:return str(0)
while num !=0:
rs.append(str(num%2))
num = num //2
rs.reverse()
return "".join(rs)
# s = "1011"
# res = Solution.binaryToDecimal(1,s)
a = "1010" ; b = "1011"
a = "0";b="0"
res = Solution.addBinary(1,a,b)
print(res)
num = 11
Solution.decimalToBinary(1,num)