C++ 0ms updated solution - No need to check positive or negative


  • 1
    A
    class Solution {
    public:
        int getSum(int a, int b) {
            if (a == 0) return b;
            if (b == 0) return a;
            // x > 0 and y > 0
            while (b){
                int x = a ^ b, y = (a & b) << 1;
                a = x; b = y;
            }
            return a;   
        }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.