The Blind 75 Leetcode Series: Sum of Two Integers
Today, we are working on 371. Sum of Two Integers
Given two integers
a
andb
, return the sum of the two integers without using the operators+
and-
.
This is a simple question, but the constraint is what is annoying. We have 2 integers which we want to find the sum of, but we are not allowed to use plus or minus.
We can check with interviewer about some other constraints:
- Are we allowed to use other operators?
- Are we allowed to transform the integers?
- Are we looking at both positive and negative integers?
If the interviewer tells us we are allowed to do anything else, as long as we are not using +
and -
, then we are good. Also, we are told we will see a range of positive and negative numbers.
Since we are coding in python, the first solution is using a loop hole of the rule. We are using sum
to deal with the problem.
def getSum(a, b):
return sum([a,b])
This is a solution, but most likely not something they are looking for. Can we come up with another solution?
In order to avoid plus and minus sign, we need another way to combine the values of the two integers.