leetCode刷题记录48_371_Sum of Two Integers

/*****************************************************问题描述*************************************************
Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -.
Example 1:
    Input: a = 1, b = 2
    Output: 3
Example 2:
    Input: a = -2, b = 3
    Output: 1
不使用+运算符,计算两个数的和。用位运算,下面是我在网上找的算法,位运算还需加强学习    
/*****************************************************我的解答*************************************************
/**
 * @param {number} a
 * @param {number} b
 * @return {number}
 */
var getSum = function(a, b) {
    if (b === 0) {
        return a;
    }
    // 不用进位的相加
    let c = a ^ b;
    // 记录需要进位的
    let d = a & b;
    d = d << 1;
    // 继续相加,直到d进位为0
    return arguments.callee(c, d);
};

发布了135 篇原创文章 · 获赞 10 · 访问量 6375

猜你喜欢

转载自blog.csdn.net/gunsmoke/article/details/88218479
今日推荐