Leetcode 202. Happy Number

Mingyu Jeon·2020년 4월 27일
0
post-thumbnail

# Runtime: 28 ms, faster than 91.32% of Python3 online submissions for Happy Number.
# Memory Usage: 14 MB, less than 5.26% of Python3 online submissions for Happy Number.

class Solution:
    def isHappy(self, n: int) -> bool:
        nums = list(str(n))
        total = 0
        hashMap = {}

        while total != 1:
            for num in nums:
                total += int(num) ** 2
            if total == 1: return True
            if hashMap.get(total, 0):
                return False
            else:
                hashMap[total] = True
            nums = list(str(total))
            total = 0

https://leetcode.com/problems/happy-number/

0개의 댓글