A Swift Solution


  • 0
    T
        var middle:[Int:Bool] = [:]
        func isHappy(n: Int) -> Bool {
            var n = n
            if n == 1 {
                return true
            }
            
            if middle[n] != nil {
                return false
            }
                
            middle[n] = true
            
            var nextN = 0
            while n != 0 {
                nextN += (n % 10) * (n % 10)
                n = n / 10
            }
            return isHappy(nextN)
        }
    

Log in to reply
 

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