您的位置:首页 > 其它

LeetCode 371. Sum of Two Integers不用加减实现加法

2016-07-15 10:23 465 查看
/[b]***********************************************[/b]

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

Example:

Given a = 1 and b = 2, return 3.

[b]**********************************[/b]/

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