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

 

上一篇:C# list中ConvertAll的使用


下一篇:CF1203C Common Divisors