My Accepted Simple Python Solution


  • 0
    R
    def isHappy(self, n):
        while n > 4:
            res = 0
            while n:
                res = res + (n % 10)**2
                n /= 10  
            n = res    
        return n == 1
    

    We can make it more simple, but it takes 4ms langer than the method above

    def isHappy(self, n):
        res = 0
        while n:
            res = res + (n % 10)**2
            n /= 10  
        n = res    
        return self.isHappy(n) if n > 4 else n ==1

Log in to reply
 

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