leetcode67 python binary summation

Given two binary strings, return their sum (in binary representation).

The input is a non- empty string and contains only numbers  1 and  sums 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
        """
        intNum=int(a,2)+int(b,2)# Convert binary to decimal, then sum
        return bin(intNum)[2:]# When the decimal is converted to binary, the first two digits are 0b, which needs to be removed
        



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325687884&siteId=291194637