Add Two Integers (LeetCode 2235): Solution and Explanation
Given two integers num1 and num2, each between -100 and 100, return their sum.
Intuition
This is the warm-up problem on LeetCode, and the answer is one line: return num1 + num2. It is useful mostly for checking that you know how the judge calls your function and what it expects back.
A number line is a good way to picture what addition means, especially with negatives. Start on num1. If num2 is positive, move right by that many units. If it is negative, move left. Where you stop is the sum.
Splitting the move into jumps of ten and steps of one mirrors how people add in their heads: 12 + 5 is one short walk, while -45 + 87 is eight jumps of ten and seven single steps.
Overflow is not a concern. The largest possible answer is 200 and the smallest is -200, far inside a 32-bit integer.
Approach: Built-in addition
- Read
num1andnum2. - Compute
num1 + num2with the language's addition operator. - Return the result.
Step-by-step example
num1 = 12, num2 = 5
- Stand on 12 on the number line.
- num2 is 5, which is positive, so move right. It has no tens and five ones.
- Take five single steps: 13, 14, 15, 16, 17.
- The marker stops on 17, so the function returns 17.
Code: Python, Java, C++
1class Solution:
2 def sum(self, num1: int, num2: int) -> int:
3 return num1 + num21class Solution {
2 public int sum(int num1, int num2) {
3 return num1 + num2;
4 }
5}1class Solution {
2public:
3 int sum(int num1, int num2) {
4 return num1 + num2;
5 }
6};Time and space complexity
O(1)
One addition of two fixed-width integers, no matter what the values are.
O(1)
No extra memory beyond the return value.
Other approaches
Bitwise addition without + (time O(1), space O(1))
XOR gives the sum without carries and AND shifted left gives the carries. Repeat until the carry is zero. This is the follow-up idea behind Sum of Two Integers and shows how hardware adders work.
Counting steps in a loop (time O(|num2|), space O(1))
Start at num1 and add or subtract 1, |num2| times. It matches the number line picture exactly but does up to 100 iterations for what one instruction can do.
Common mistakes
- Returning a string or printing the value instead of returning an integer from the method.
- Changing the method name or signature. The judge calls
sum(num1, num2)exactly, orSumin C#. - Overthinking negatives. The + operator already handles mixed signs, so no special cases are needed.
Frequently asked questions
Why does such a simple problem exist?
It is an onboarding problem. It lets you see how the editor, the function signature and the judge fit together before you spend effort on a harder problem.
Can num1 + num2 overflow?
No. Both inputs are between -100 and 100, so the sum is between -200 and 200, which every integer type holds easily.
How would I add without using +?
Use bit operations: a ^ b adds without carries and (a & b) << 1 produces the carries. Loop until the carry is zero. The Sum of Two Integers problem asks for exactly this.