Largest Rectangle in Histogram (Python)
Sqrt(x) @ LeetCode (Python)

Add Binary (Python)

kitt posted @ 2014年2月05日 19:18 in LeetCode , 2288 阅读

 

class Solution:
    # @param a, a string
    # @param b, a string
    # @return a string
    def addBinary(self, a, b):
        length_a = len(a)
        length_b = len(b)
        if length_a > length_b:
            b = '0' * (length_a - length_b) + b
            length = length_a
        else:
            a = '0' * (length_b - length_a) + a
            length = length_b
        a = a[::-1]
        b = b[::-1]
        Sum = ''
        carry = 0
        for i in xrange(length):
            tmp = ord(a[i]) - 48 + ord(b[i]) - 48 + carry
            Sum += str(tmp % 2)
            carry = tmp / 2
        if carry == 1:
            Sum += '1'
        return Sum[::-1]

登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter