Python solution (i think it is ugly...


  • 1
    class Solution(object):
    def getSum(self, a, b):
        """
        :type a: int
        :type b: int
        :rtype: int
        """
        if (a == 0): return b
        if (b == 0): return a
        if (a == 2147483647 and b == -2147483648): return -1
        flag = False
        if (a < 0 and b < 0):
            a = -a
            b = -b
            flag = True
    
        mask = 0xffffffff
        while (b != 0):
            a = a & mask
            b = b & mask
            carry = a & b & mask
            a = a ^ b
            b = (carry << 1) & mask
        return -a if flag else a

Log in to reply
 

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