Scribbling

LeetCode: 202. Happy Number 본문

Computer Science/Coding Test

LeetCode: 202. Happy Number

focalpoint 2021. 12. 21. 20:25
class Solution:
    def isHappy(self, n: int) -> bool:
        def calc(x):
            ret = 0
            while x > 0:
                ret += (x % 10)**2
                x //= 10
            return ret
        
        numSet = set([n])
        x = n
        while True:
            x = calc(x)
            if x == 1:
                return True
            if x in numSet:
                return False
            numSet.add(x)