A neat and easy solution based on recursion & bit maipulation


  • 0
    W
    class Solution {
    public:
        int getSum(int a, int b) {
            if (a == 0 || b == 0)
                return a | b;
    
            return getSum(a^b, ((a&b) << 1)); // para --> a: sum without carry ,b : the carry
        }
    };
    

    The end of this solution must be the situation that either a or b equals to zero. And then, simply return the sum and we will get the answer.


Log in to reply
 

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