程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python leetcode67: binary sum

編輯:Python

subject :

Here are two binary strings , Returns the sum of them ( In binary ).

Input is   Non empty   String and contains only numbers  1  and  0.

for example :

Input : a = "11", b = "1" Output : "100"

solution :

Consider the simplest approach : First the  a  and  b  Convert to decimal , Sum and convert to binary numbers .

class Solution:
def addBinary(self, a: str, b: str) -> str:
return bin(int(a, 2) + int(b, 2))[2:]
#int(x, base=10),base The default is 10 Base number
# bin() Returns an integer int Or long integers longint The binary representation of .
a = "11"
b = "1"
S = Solution()
result = S.addBinary(a,b)
print(result)


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved