Sum of Two Integers
LeetCode 371
class Solution:
def getSum(self, a, b):
"""
:type a: int
:type b: int
:rtype: int
"""
mask = 0xFFFFFFFF
maxi = 0x7FFFFFFF
"""
011 3
010 2
001 1
000 0
111 -1
110 -2
101 -3
"""
if b ==0 and a <= maxi:
return a
elif b == 0 and a > maxi:
return ~(a ^ mask)
else:
s = (a ^ b) & mask
c = ((a & b) << 1) & mask
return self.getSum(s, c)
Last updated