One line Javascript solution beaten 93% submissions


  • 0
    N

    Explanation:

    1. a ^ b finds the sum without carry over
    2. a & b finds the remain carry overs
    3. (a & b) << 1 moves the carry overs to the correct position
    4. recursively sum results from 1 and 3 until 2 is zero.
    var getSum = function(a, b) {
        return ((a & b ) != 0) ? getSum(a ^ b, (a & b) << 1) : (a ^ b);
    };
    

Log in to reply
 

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