Python solution


  • 1
    J
    class Solution:
    # @param {integer} n
    # @return {boolean}
    def isHappy(self, n):
        table = []
        n = self.convert(n)
        while n != 1:
            if n in table:
                return False
            else:
                table.append(n)
                n = self.convert(n)
        return True
    
    # @param {integer} n
    # @return {integer} sum of digits
    def convert(self, n):
        res = 0
        while n > 0:
            temp = n % 10
            res += temp * temp
            n = n // 10
        return res

Log in to reply
 

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