Easy to understand python solution


  • 0
    L

    As the sum of the digits will finally get a single digit ( 1~9), so I just need to consider whether the 9 numbers are happy numbers or not.

    '''

    def isHappy(self, n):
        if n == 1 or n ==7:
            return True
        elif n in [2, 3, 4, 5, 6, 8, 9]:
            return False
        else:
            squareSum = 0
            while n != 0:
                squareSum += (n%10) ** 2
                n = n // 10
            return self.isHappy(squareSum)
    

    '''


Log in to reply
 

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