Python实现"二进制求和"的四种方法

给定两个二进制字符串,返回它们的和(也是二进制字符串)

输入字符串都是非空的,并且只包含'0'和'1'字符

Example 1:

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

Example 2:

Input: a = "1010", b = "1011"
Output: "10101"

1:二进制转10进制,10进制相加之后将结果转为二进制(输入字符串均为'0'则单独判断)

class Solution(object):
    def addBinary(self, a, b):
        """
        :type a: str
        :type b: str
        :rtype: str
        """
        if a=='0' and b=='0':
            return '0'
        a = self.binaryToDecimalism(a)
        b = self.binaryToDecimalism(b)
        result = self.decimalismToBinary(a+b)
        return result

    def binaryToDecimalism(self, binary):
        sum = 0
        for index,num in enumerate(binary):
            sum += int(num)*(2**(len(binary)-index-1))
        return sum

    def decimalismToBinary(self, Decimalism):
        binary = ""
        while Decimalism > 0:
            restNum = Decimalism%2
            binary = str(restNum) + binary
            Decimalism = Decimalism//2
        return binary

2:字符串二进制加法,以长的二进制字符串为主遍历串

def addBinary(self, a, b):
        """
        :type a: str
        :type b: str
        :rtype: str
        """
        if len(a) < len(b):    #以长的二进制字符串为遍历起点
            temp = a
            a = b
            b = temp
        a = a[::-1]           #倒序二进制字符串
        b = b[::-1]
        extra = 0             #进位
        new_binary = ""
        for index, num in enumerate(a):     #遍历
            if index > len(b) - 1:          #判断短的二进制字符串是否越界
                b_sum = 0
            else:
                b_sum = int(b[index])
            new_binary = new_binary + str((int(num) + b_sum + extra) % 2)     #二进制加法运算
            if int(num) + b_sum + extra > 1:     #是否进位
                extra = 1
            else:
                extra = 0
        if extra == 1:        #最高位是否进位
            new_binary = new_binary + "1"     
        return new_binary[::-1]    #倒序输出

3:字符串二进制加法,将两个二进制字符串变为等长

def addBinary(self, a, b):
        """
        :type a: str
        :type b: str
        :rtype: str
        """
        if len(a) < len(b):    #填充"0",将两个字符串变为等长
            temp = a
            a = b
            b = temp
        a = a[::-1]           #倒序二进制字符串
        b = b[::-1]
        while len(a)!=len(b):     #二进制字符串长度设置相同
            b = b+"0"
        extra = 0             #进位
        new_binary = ""
        for index, num in enumerate(a):     #遍历
            b_sum = int(b[index])
            new_binary = new_binary + str((int(num) + b_sum + extra) % 2)     #二进制加法运算
            if int(num) + b_sum + extra > 1:     #是否进位
                extra = 1
            else:
                extra = 0
        if extra == 1:        #最高位是否进位
            new_binary = new_binary + "1"     
        return new_binary[::-1]    #倒序输出

4:利用int()和bin(),将二进制转十进制,十进制相加之后转二进制

def addBinary(self, a, b):
        """
        :type a: str
        :type b: str
        :rtype: str
        """
        a = int(a,2)
        b = int(b,2)
        return bin(a+b)[2:]

算法题来自:https://leetcode-cn.com/problems/add-binary/description/

猜你喜欢

转载自blog.csdn.net/qiubingcsdn/article/details/82263114
今日推荐