-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path371-sum-of-two-integers.py
More file actions
40 lines (32 loc) · 1.02 KB
/
371-sum-of-two-integers.py
File metadata and controls
40 lines (32 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
'''
Calculate the sum of two integers a and b, but you are not allowed to use the
operator + and -.
'''
class Solution(object):
def getSum(self, a, b):
"""
:type a: int
:type b: int
:rtype: int
"""
maxInt = 0x7FFFFFFF # max 32-bit int
mask = 0xFFFFFFFF # 32-bit number consisting of all 1s,
# used to get last 32 bits of Python's 64-bit integers
if b == 0:
if a <= maxInt:
return a
else:
return ~(a ^ mask) # return 32-bit complement
if a == 0:
return b
# bitwise XOR: partial solution before carrying digits
partialSolution = a ^ b
# bitwise AND: digits that need carrying (i.e. 1 digits in both numbers)
digitsToCarry = a & b
# left-shift toCarry by 1: carried digits to be added to solution
carriedDigits = digitsToCarry << 1
partialSolution = partialSolution & mask
carriedDigits = carriedDigits & mask
# recursively call until all carried digits have been correctly placed,
# i.e. continue until carriedDigits == 0
return self.getSum(partialSolution, carriedDigits)