371. sum of two integers 实现 参考资料

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

  • Example 1:
1
2
Input: a = 1, b = 2
Output: 3

-Example 2:

1
2
Input: a = -2, b = 3
Output: 1

不用 +,-运算符计算两个 int 值的和.

实现

  • java

递归位运算

1
2
3
public int (int a, int b) {
return (b == 0) ? a : getSum(a ^ b, (a & b) << 1);
}

参考资料