Skip to main content

202. Happy Number

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

Python

class Solution:
def isHappy(self, n: int) -> bool:
seen = set()

while n != 1 and n not in seen:
seen.add(n)
current, m = 0, n
while m > 0:
m, remain = divmod(m, 10)
current += remain ** 2
n = current

return n == 1