码迷,mamicode.com
首页 > 其他好文 > 详细

[LeetCode] 371. Sum of Two Integers

时间:2019-10-17 23:46:17      阅读:98      评论:0      收藏:0      [点我收藏+]

标签:ref   span   function   思路   and   com   htm   blog   ===   

题意是不用加减法做到对两个整数求和。这里我参考了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 };

 

[LeetCode] 371. Sum of Two Integers

标签:ref   span   function   思路   and   com   htm   blog   ===   

原文地址:https://www.cnblogs.com/aaronliu1991/p/11695656.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!