Leetcode - 371. Sum of Two Integers

Posted kaezah

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Leetcode - 371. Sum of Two Integers相关的知识,希望对你有一定的参考价值。

题设:

Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -.

解题思路:

使用加法器

代码:

class Solution:
    def getSum(self, num1, num2):
        if (num2 == 0):
            return num1
        sum = num1 ^ num2
        carry = (num1 & num2) << 1
        return self.getSum(sum,carry)

结果:

python本地运行:通过

run code运行:通过,运行时间32ms

submit结果:不通过,报“超出内存限制”

以上是关于Leetcode - 371. Sum of Two Integers的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode 第 371 题 (Sum of Two Integers)

Leetcode - 371. Sum of Two Integers

leetcode-371-Sum of Two Integers

LeetCode 371. Sum of Two Integers

Leetcode:371.Sum Of Two Integer

[leetcode] 371. Sum of Two Integers 解题报告