Python solution


  • 0
    T

    Count each bit as int

    class Solution(object):
        def addBinary(self, a, b):
            """
            :type a: str
            :type b: str
            :rtype: str
            """
            af=0
            res=""
            i=1
            while i<=min(len(a),len(b)):
            	tmp=int(a[-i])+int(b[-i])+af
            	if tmp>=2:
            		tmp-=2
            		af=1
            	else:
            		af=0
            	res=str(tmp)+res
            	i+=1
            while i<=len(a):
            	tmp=int(a[-i])+af
            	if tmp>=2:
            		tmp-=2
            		af=1
            	else:
            		af=0
            	res=str(tmp)+res
            	i+=1
            while i<=len(b):
            	tmp=int(b[-i])+af
            	if tmp>=2:
            		tmp-=2
            		af=1
            	else:
            		af=0
            	res=str(tmp)+res
            	i+=1
            if af>0:
            	res=str(af)+res
            return res
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.