My python solution:


  • 0
    A
    def isHappy(self, n):
        """
        :type n: int
        :rtype: bool
        """
        if n == 1:
            return True
    
        seen = set()        
        
        while n not in seen:
            seen.add(n)
            n = self.calculate(n)
        
        if n == 1:
            return True
        else:
            return False
                
    def calculate(self, num):
        ret = 0
    
        while num:
            ret += (num%10)**2
            num = num//10
        
        return ret

Log in to reply
 

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