您的位置:首页 > 其它

[LeetCode]371.Sum of Two Integers两数相加

2017-05-09 12:58 417 查看
371.

Calculate the sum of two integers a and b,
but you are not allowed to
use the operator 
+
 and 
-
.

两数相加,不使用加减符号。

使用异或、与、位移运算符。

class Solution {
public:
int getSum(int a, int b)
{
if (b == 0)
return a;
else {
int sum = a^b;
int temp = (a&b) << 1;
return (getSum(sum, temp));
}
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  leetcode