leetcode 67. Add Binary Python快速實現

題目 leetcode 67. Add Binary Python
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(object):
    def addBinary(self, a, b):
        """
        :type a: str
        :type b: str
        :rtype: str
        """
        ten_sum =  int(a,2) +  int(b,2)#二進制到十進制轉化函數int(x,2)
        
        return bin(ten_sum)[2:]#十進制到二進制轉化函數bin()

思路:Python內部有很多有用的函數,直接調用就可以很好的解決問題。人生苦短,我用Python。
在這裏插入圖片描述
應該是我見過的解決這個問題最短的代碼,速度也是很快,提交的時候超過100%的已提交代碼。有最新的麻煩評論處貼一下鏈接。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章