class Solution {
public:
int getSum(int a, int b) {
int base = a ^ b;
int c[32];
c[0] = 0;
for(int i = 1; i < 32; i++) {
c[i] = (c[i  1] && (a & 1  b & 1))  (a & 1 && b & 1);
a >>= 1;
b >>= 1;
}
int carry = 0;
for(int i = 31; i >= 0; i) {
carry = (carry << 1) + c[i];
}
return base ^ carry;
}
};
0ms Simple C++ Solution




 Eh, thanks to @kid1412z ,I find something wrong in my code. There is an alternative one, which looks not simple though.
class Solution { public: int getSum(int a, int b) { int base = a ^ b, carry = 0; stack<int> c; c.push(0); while(c.size() != 32) { int tmp = (c.top() && (a & 1  b & 1))  (a & 1 && b & 1); c.push(tmp); a >>= 1; b >>= 1; } while(!c.empty()) { carry = (carry << 1)  c.top(); c.pop(); } return base ^ carry; } };

@amoy_lan said in 0ms Simple C++ Solution:
@kid1412z I just find I use + and  in the loop, this is not a proper solution.
No, you use
++
and
. Those aren't forbidden. And you're not even using them ona
orb
. In my personal opinion, it's ok, you're following both the letter and the spirit of the problem specification (except for what @kid1412z pointed out).But ha, just saw your replacement. Very creative.
