题目来源:a-b-problem
题目描述:
给出两个整数a和b, 求他们的和, 但不能使用 + 等数学运算符。
样例
如果 a=1 并且 b=2,返回3
Java代码:
class Solution {
/*
* param a: The first integer
* param b: The second integer
* return: The sum of a and b
*/
public int aplusb(int a, int b) {
// write your code here, try to do it without arithmetic operators.
if(b==0)
return a;
int c = (a & b)<<1, r = a ^ b;
return aplusb(r, c);
}
};