Explore diverse LeetCode solutions in Python, C++, JavaScript, SQL, and TypeScript. Ideal for interview prep, learning, and code practice in multiple programming languages. Github Repo Link
Given two integers a
and b
, return the sum of the two integers without using the operators +
and -
.
Example 1:
Input: a = 1, b = 2 Output: 3
Example 2:
Input: a = 2, b = 3 Output: 5
Constraints:
-1000 <= a, b <= 1000
Python
# time complexity: O(1) # space complexity: O(1) class Solution: def getSum(self, a: int, b: int) -> int: x, y = abs(a), abs(b) # ensure that abs(a) >= abs(b) if x < y: return self.getSum(b, a) # abs(a) >= abs(b) --> # a determines the sign sign = 1 if a > 0 else -1 if a * b >= 0: # sum of two positive integers x + y # where x > y while y: answer = x ^ y carry = (x & y) << 1 x, y = answer , carry else: # difference of two integers x - y # where x > y while y: answer = x ^ y borrow = ((~x) & y) << 1 x, y = answer, borrow return x * sign a = 1 b = 2 print(Solution().getSum(a, b)) velocities = [0, 0.5, 1, 1.5] movement = velocities[1:3] print(movement)