Simple and naive, yet fast


  • 0

    Python recursion, beat 96.49% and acc at 39 ms.

    class Solution(object):
        def isHappy(self, n):
            def is_happy(n):
                p = (n == 1) + (n == 7) + (n < 10)
                if p == 2:
                    return True
                if p == 0:
                    return is_happy(sum(int(x)**2 for x in str(n)))
                else:
                    return False
            return is_happy(n)

Log in to reply
 

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