My python solution through recursion


  • 0
    C
    def isHappy(self, n):
        """
        :type n: int
        :rtype: bool
        """
        d = set()
        def inner(n):
            if n < 1 or n in d:
                return False
            if n == 1:
                return True
            su = 0
            w = n
            while n:
                su += (n % 10) ** 2
                n = int(n / 10)
            d.add(w)
            return inner(su)
        return inner(n)

Log in to reply
 

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