Problem
Given two integers num1
and num2
, return the sum of the two integers.
Examples
Example 1:
|
|
Example 2:
|
|
Constraints:
-100 <= num1, num2 <= 100
Solution
Method 1 – Simple Addition
Intuition
The problem asks for the sum of two integers. The key idea is that addition is a basic arithmetic operation, and in all programming languages, the +
operator can be used to add two numbers directly. Since the constraints are small and there are no edge cases beyond the given range, a direct addition suffices.
Approach
- Receive two integer inputs,
num1
andnum2
. - Add them using the
+
operator. - Return the result.
This approach works for all valid integer inputs, including negative numbers and zero.
Code
|
|
|
|
|
|
Complexity
- ⏰ Time complexity:
O(1)
— Only a single addition operation is performed. - 🧺 Space complexity:
O(1)
— No extra space is used beyond a single variable.