1. Description
2. Solution
- Version 1
class Solution {
public:
int getSum(int a, int b) {
int sum = 0;
int carry = 0;
while(b)
{
sum = a ^ b;
carry = a & b;
a = sum;
b = carry << 1;
}
return sum;;
}
};
- Version 2
class Solution {
public:
int getSum(int a, int b) {
return b == 0 ? a : getSum(a ^ b, (a & b) << 1);
}
};