5 lines Python


  • 1

    Using a set to remember where we've been, others are pretty straight forward.

    class Solution(object):
        def isHappy(self, n):
            """
            :type n: int
            :rtype: bool
            """
            visit = set()
            while n not in visit and n != 1:
                visit.add(n)
                n = sum(int(i) ** 2 for i in str(n))
            return n == 1
    

Log in to reply
 

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