Skip to content

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

Thinking

Add two integers in \([-100,100]\). The language's addition operator already does this in constant time; no carry handling is required.

1
2
3
class Solution:
    def sum(self, num1: int, num2: int) -> int:
        return num1 + num2
1
2
3
4
5
class Solution {
    public int sum(int num1, int num2) {
        return num1 + num2;
    }
}
1
2
3
4
5
6
class Solution {
public:
    int sum(int num1, int num2) {
        return num1 + num2;
    }
};
1
2
3
func sum(num1 int, num2 int) int {
    return num1 + num2
}
1
2
3
function sum(num1: number, num2: number): number {
    return num1 + num2;
}
1
2
3
4
5
impl Solution {
    pub fn sum(num1: i32, num2: i32) -> i32 {
        num1 + num2
    }
}
1
2
3
int sum(int num1, int num2) {
    return num1 + num2;
}

Solution 2

Thinking

Solution 1 uses the addition operator. Without it we simulate grade-school addition on bits: xor is the sum without carry, and the carry is the bitwise and shifted left, repeated until the carry vanishes.

Python integers are unbounded, so we mask to \(32\) bits with \(0\texttt{xFFFFFFFF}\). A set sign bit is converted back to a negative Python int via two's complement.

1
2
3
4
5
6
7
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)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
class Solution {
    public int sum(int num1, int num2) {
        while (num2 != 0) {
            int carry = (num1 & num2) << 1;
            num1 ^= num2;
            num2 = carry;
        }
        return num1;
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
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;
    }
};
1
2
3
4
5
6
7
8
func sum(num1 int, num2 int) int {
    for num2 != 0 {
        carry := (num1 & num2) << 1
        num1 ^= num2
        num2 = carry
    }
    return num1
}
1
2
3
4
5
6
7
8
function sum(num1: number, num2: number): number {
    while (num2) {
        const carry = (num1 & num2) << 1;
        num1 ^= num2;
        num2 = carry;
    }
    return num1;
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
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
    }
}

Comments