zoukankan      html  css  js  c++  java
  • Add Binary

    class Solution:
        # @param a, a string
        # @param b, a string
        # @return a string
        def addBinary(self, a, b):
            #长度不等,补齐
            len_a=len(a)
            len_b=len(b)
            if len_a>len_b:
                for i in range(len_a-len_b):
                    b='0'+b
                len_b=len_a
            else:
                for i in range(len_b-len_a):
                    a='0'+a
                len_a=len_b
    
            res=""
            carry='0'
    
            for i in range(len_a):
                sum=self.addsingleBinary(a[len_a-i-1],b[len_b-i-1],carry)
                if sum=='3':
                    res='1'+res
                    carry='1'
                    continue
                if sum=='2':
                    res='0'+res
                    carry='1'
                    continue
                else:
                    res=sum+res
                    carry='0'
                    continue
            if carry=='1':
                res='1'+res
            return res
    
        #单个二进制数以及前一位之和的进数相加
        def addsingleBinary(self,x,y,carry):
            if carry=='1':
                if x=='1' and y=='1':
                    return '3'
                if x=='0' and y=='0':
                    return '1'
                else:
                    return '2'
            if carry=='0':
                if x=='1' and y=='1':
                    return '2'
                if x=='0' and y=='0':
                    return '0'
                else:
                    return '1'
    
    if __name__ == '__main__':
        s=Solution()
        print(s.addBinary("111","110"))
  • 相关阅读:
    小涛涛的计算器
    Sort排序浅聊
    程序员PC选购
    冒泡排序
    Django之模板
    Django中model的Meta选项
    Django之路由系统
    Django之视图系统
    踏上Flask的不归路(二)
    踏上Flask的不归路(一)
  • 原文地址:https://www.cnblogs.com/iois/p/4024502.html
Copyright © 2011-2022 走看看