Solution for python


  • 0
    class Solution(object):    
        def isHappy(self, n):
            """
            :type n: int
            :rtype: bool
            """
            if n == 1:
                return True
            n = str(n)
            s = set()
            while int(n) != 1:            
                j = 0
                for i in n:
                    j += int(i)**2
                if j == 1:
                    return True
                
                if j in s:
                    break
                else:
                    s.add(int(n))
                n = str(j)
            return int(n) == 1
    

Log in to reply
 

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