Simple PYTHON 49 ms 90th%


  • 0
    V
    class Solution(object):
        def isHappy(self, n):
            """
            :type n: int
            :rtype: bool
            """
            if n == 1: return True
            map = {n: 1}
            ptr = n
            
            while ptr != 1:
                ptrs = str(ptr)
                ptr = 0
                for c in ptrs:
                    ptr += int(c) ** 2
                
                if ptr in map:
                    return False
                else:
                    map[ptr] = 1
    
            return True
    

Log in to reply
 

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