본문 바로가기
Leetcode

[LeetCode] Happy Number 파이썬 (dict, cycle)

by YGSEO 2021. 4. 4.
728x90
class Solution:
    def isHappy(self, n):
        lookup = {}
        while n != 1 and n not in lookup:
            lookup[n] = True
            n = self.nextNumber(n)
        return n == 1

    def nextNumber(self, n):
        new = 0
        for char in str(n):
            new += int(char)**2
        return new
        

 

 

출처: github.com/jiapengwen/LeetCode/blob/master/Python/happy-number.py

728x90

댓글