Description:
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.
solution:
public static int getSum(int a, int b) {
if (b == 0) {
return a;
}
int sum, carry;
sum = a ^ b;
carry = (a & b) << 1;
return getSum(sum, carry);
}
以上代码有一点我不明白,应该如何准确地解释边界情况是b==0。
希望大神指教。
...全文
3502打赏收藏
关于Sum of Two Integers(LeetCode 371)边界情况的疑惑
Description: 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. solution: public static int getSum(int a, int b) { if (b == 0) { return a; } int sum, carry; sum =