Python simple solution


  • 0
    class Solution(object):
        def getHappy(self, n):
            if n / 10 == 0:
                return n ** 2
            
            tmp = 0
            while n != 0:
                digit = n % 10
                tmp += digit ** 2
                n = n / 10
            return tmp
        
        def isHappy(self, n):
            history = {}
            happy = self.getHappy(n)
            while happy != 1:
                history[happy] = True
                happy = self.getHappy(happy)
                
                if happy in history:
                    return False
            return True

Log in to reply
 

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