其他分享
首页 > 其他分享> > [LeetCode] 371. Sum of Two Integers

[LeetCode] 371. Sum of Two Integers

作者:互联网

题意是不用加减法做到对两个整数求和。这里我参考了grandyang大神的思路,就不过多解释了,https://www.cnblogs.com/grandyang/p/5631814.html

时间O(1)

空间O(1)

 1 /**
 2  * @param {number} a
 3  * @param {number} b
 4  * @return {number}
 5  */
 6 var getSum = function(a, b) {
 7     if (a === 0) {
 8         return b;
 9     }
10     if (b === 0) {
11         return a;
12     }
13     while (b !== 0) {
14         let carry = a & b;
15         a = a ^ b;
16         b = carry << 1;
17     }
18     return a;
19 };

 

标签:Integers,return,题意,Sum,number,param,grandyang,carry,371
来源: https://www.cnblogs.com/aaronliu1991/p/11695656.html