Swift Easy and Short Recursive Solution


  • 0
    Y
    func isHappy(_ n: Int) -> Bool {
        var num = n
        var next = 0
        while num>=1{
            next += Int(pow(Double(num % 10), 2))
            num /= 10
        }
        if next == 1 { return true }
        return next == 4 ? false : isHappy(next)   
    }

Log in to reply
 

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