Leetcode 2235. Add Two Integers
You are given two integers num1
and num2
representing two integers. Implement a function that returns the sum of num1
and num2
.
Example:
Input: num1 = 12, num2 = 5
Output: 17
Input: num1 = -10, num2 = 4
Output: -6
num1
and num2
guaranteed to be non-null?
num1
and num2
be negative?
num1
and num2
?
The problem is straightforward. The task is to return the sum of the two integers. This can be achieved by simply using the +
operator in Java.
public class Solution {
public int sum(int num1, int num2) {
return num1 + num2;
}
public static void main(String[] args) {
Solution solution = new Solution();
System.out.println(solution.sum(12, 5)); // Output: 17
System.out.println(solution.sum(-10, 4)); // Output: -6
}
}
The time complexity for this function is O(1) because adding two integers is a constant time operation.
The space complexity is O(1) since no extra space is used beyond the input integers and the output value.
If you have any further questions or need additional examples, feel free to ask!
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?