[LeetCode] 371. Sum of Two Integers
Posted aaronliu1991
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[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 };
以上是关于[LeetCode] 371. Sum of Two Integers的主要内容,如果未能解决你的问题,请参考以下文章
LeetCode 第 371 题 (Sum of Two Integers)
Leetcode - 371. Sum of Two Integers
leetcode-371-Sum of Two Integers
LeetCode 371. Sum of Two Integers