Welcome to Subscribe On Youtube
2235. Add Two Integers
Description
Given two integers num1 and num2, return the sum of the two integers.
Example 1:
Input: num1 = 12, num2 = 5 Output: 17 Explanation: num1 is 12, num2 is 5, and their sum is 12 + 5 = 17, so 17 is returned.
Example 2:
Input: num1 = -10, num2 = 4 Output: -6 Explanation: num1 + num2 = -6, so -6 is returned.
Constraints:
-100 <= num1, num2 <= 100
Solutions
Solution 1: Direct Implementation
We can directly use the addition operator + to calculate the sum of two integers.
Time complexity $O(1)$, space complexity $O(1)$.
Solution 2
We can also use bitwise operations to calculate the sum of two integers without using the addition operator.
Assume that $num1_i$ and $num2_i$ represent the $i$th binary bit of $num1$ and $num2$ respectively. There are a total of $4$ situations:
| $num1_i$ | $num2_i$ | Sum without carry | Carry |
|---|---|---|---|
| 0 | 0 | 0 | 0 |
| 0 | 1 | 1 | 0 |
| 1 | 0 | 1 | 0 |
| 1 | 1 | 0 | 1 |
Observation shows that “sum without carry” has the same rules as “XOR operation”, while carry has the same rules as “AND” operation, and needs to be shifted one bit to the left.
Therefore:
- Perform the bitwise AND operation
&on the two numbers, and then shift one bit to the left to get the carry bit, which is recorded as $carry$; - Perform bitwise
^XOR operation on two numbers to obtain the sum without carry; - The problem is transformed into finding: the sum of “number without carry + carry”;
- Loop until the second number is $0$ and return the first number (it can also be implemented using recursion).
The time complexity is $O(\log M)$, where $M$ is the maximum value of the numbers in the question. Space complexity $O(1)$.
-
class Solution { public int sum(int num1, int num2) { return num1 + num2; } } // Solution 2 class Solution { public int sum(int num1, int num2) { while (num2 != 0) { int carry = (num1 & num2) << 1; num1 ^= num2; num2 = carry; } return num1; } } -
class Solution { public: int sum(int num1, int num2) { return num1 + num2; } }; // Solution 2 class Solution { public: int sum(int num1, int num2) { while (num2) { unsigned int carry = (unsigned int) (num1 & num2) << 1; num1 ^= num2; num2 = carry; } return num1; } }; -
class Solution: def sum(self, num1: int, num2: int) -> int: return num1 + num2 # Solution 2 class Solution: def sum(self, num1: int, num2: int) -> int: num1, num2 = num1 & 0xFFFFFFFF, num2 & 0xFFFFFFFF while num2: carry = ((num1 & num2) << 1) & 0xFFFFFFFF num1, num2 = num1 ^ num2, carry return num1 if num1 < 0x80000000 else ~(num1 ^ 0xFFFFFFFF) -
func sum(num1 int, num2 int) int { return num1 + num2 } // Solution 2 func sum(num1 int, num2 int) int { for num2 != 0 { carry := (num1 & num2) << 1 num1 ^= num2 num2 = carry } return num1 } -
function sum(num1: number, num2: number): number { return num1 + num2; } // Solution 2 function sum(num1: number, num2: number): number { while (num2) { const carry = (num1 & num2) << 1; num1 ^= num2; num2 = carry; } return num1; } -
impl Solution { pub fn sum(num1: i32, num2: i32) -> i32 { num1 + num2 } } // Solution 2 impl Solution { pub fn sum(num1: i32, num2: i32) -> i32 { let mut num1 = num1; let mut num2 = num2; while num2 != 0 { let carry = (num1 & num2) << 1; num1 ^= num2; num2 = carry; } num1 } } -
int sum(int num1, int num2) { return num1 + num2; }