MCPcopy Index your code
hub / github.com/neetcode-gh/leetcode / getSum

Method getSum

python/0371-sum-of-two-integers.py:2–16  ·  view source on GitHub ↗
(self, a: int, b: int)

Source from the content-addressed store, hash-verified

1class Solution:
2 def getSum(self, a: int, b: int) -> int:
3 def add(a, b):
4 if not a or not b:
5 return a or b
6 return add(a ^ b, (a & b) << 1)
7
8 if a * b < 0: # assume a < 0, b > 0
9 if a > 0:
10 return self.getSum(b, a)
11 if add(~a, 1) == b: # -a == b
12 return 0
13 if add(~a, 1) < b: # -a < b
14 return add(~add(add(~a, 1), add(~b, 1)), 1) # -add(-a, -b)
15
16 return add(a, b) # a*b >= 0 or (-a) > b > 0

Callers

nothing calls this directly

Calls 1

addFunction · 0.50

Tested by

no test coverage detected