1 Liner SWIFT Recursive Solution


  • 0
    I

    carry = a & b
    sum of 2 bits = a ^ b
    shift left carry until carry is 0 to calculate sum

    class Solution {
        func getSum(_ a: Int, _ b: Int) -> Int {
            return b == 0 ? a : getSum(a^b, (a&b) << 1)
        }
    }

Log in to reply
 

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