列表

详情


2235. 两整数相加

给你两个整数 num1num2,返回这两个整数的和。

 

示例 1:

输入:num1 = 12, num2 = 5
输出:17
解释:num1 是 12,num2 是 5 ,它们的和是 12 + 5 = 17 ,因此返回 17 。

示例 2:

输入:num1 = -10, num2 = 4
输出:-6
解释:num1 + num2 = -6 ,因此返回 -6 。

 

提示:

原站题解

去查看

上次编辑到这里,代码来自缓存 点击恢复默认模板
class Solution { public: int sum(int num1, int num2) { } };

php 解法, 执行用时: 8 ms, 内存消耗: 18.9 MB, 提交时间: 2023-08-19 09:42:45

class Solution {

    /**
     * @param Integer $num1
     * @param Integer $num2
     * @return Integer
     */
    function sum($num1, $num2) {
        while ( $num2 ) {
            $carry = ($num1 & $num2) << 1;
            $num1 ^= $num2;
            $num2 = $carry;
        }
        return $num1;
    }
}

javascript 解法, 执行用时: 56 ms, 内存消耗: 41 MB, 提交时间: 2023-08-19 09:41:15

/**
 * @param {number} num1
 * @param {number} num2
 * @return {number}
 */
var sum = function(num1, num2) {
    while (num2) {
        const carry = (num1 & num2) << 1;
        num1 ^= num2;
        num2 = carry;
    }
    return num1;
};

rust 解法, 执行用时: 0 ms, 内存消耗: 1.9 MB, 提交时间: 2023-08-19 09:39:47

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
    }
}

typescript 解法, 执行用时: 68 ms, 内存消耗: 42.4 MB, 提交时间: 2023-08-19 09:39:22

function sum(num1: number, num2: number): number {
    while (num2) {
        const carry = (num1 & num2) << 1;
        num1 ^= num2;
        num2 = carry;
    }
    return num1;
}

cpp 解法, 执行用时: 0 ms, 内存消耗: 5.8 MB, 提交时间: 2023-08-19 09:38:41

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;
    }
};

golang 解法, 执行用时: 0 ms, 内存消耗: 1.8 MB, 提交时间: 2023-08-19 09:38:22

func sum(num1 int, num2 int) int {
	for num2 != 0 {
		carry := (num1 & num2) << 1
		num1 ^= num2
		num2 = carry
	}
	return num1
}

java 解法, 执行用时: 0 ms, 内存消耗: 38.2 MB, 提交时间: 2023-08-19 09:37:57

class Solution {
    public int sum(int num1, int num2) {
        while (num2 != 0) {
            int carry = (num1 & num2) << 1;
            num1 ^= num2;
            num2 = carry;
        }
        return num1;
    }
}

python3 解法, 执行用时: 44 ms, 内存消耗: 16 MB, 提交时间: 2023-08-19 09:37:38

class Solution:
    def sum(self, num1: int, num2: int) -> int:
        # Python 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)

golang 解法, 执行用时: 0 ms, 内存消耗: 1.8 MB, 提交时间: 2022-05-25 16:53:55

func sum(num1 int, num2 int) int {
    return (num1 & num2 ) << 1 + (num1 ^ num2)
}

python3 解法, 执行用时: 40 ms, 内存消耗: 15 MB, 提交时间: 2022-05-25 16:50:33

class Solution:
    def sum(self, num1: int, num2: int) -> int:
        return num1 + num2

上一题