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.
用 异或 表示sum,与 表示 carry,carry左移一位后,递归调用getSum(sum,carry<<1)
一刷:
public int getSum(int a, int b) { return b ==0 ? a:getSum( a^b , (a&b)<<1 ); }
[leetcode] 371. Sum of Two Integers 解题报告
原文:http://www.cnblogs.com/pulusite/p/5840489.html