LintCode-A + B 问题

给出两个整数a和b, 求他们的和, 但不能使用 + 等数学运算符。

说明:

a和b都是 32位 整数么?是的

我可以使用位运算符么?可以

样例:如果 a=1 并且 b=2,返回3

代码如下:

class Solution:
    """
    @param a: An integer
    @param b: An integer
    @return: The sum of a and b 
    """
    def aplusb(self,a, b):
        # write your code here
        while (a&b!=0):
            c=a^b
            d=(a&b)<<1
            a = c 
            b = d
        return (a|b)


 
 

猜你喜欢

转载自blog.csdn.net/u013112749/article/details/80282755