C++ 0ms easy solution


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

Log in to reply
 

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