Leetcode/python/202_happy_number.py

17 lines
406 B
Python
Raw Permalink Normal View History

2024-04-30 16:07:28 +00:00
class Solution:
def isHappy(self, n: int) -> bool:
seen = {n}
while True:
n = self.square_digital_sum(n)
if n in seen:
return n == 1
seen.add(n)
@staticmethod
def square_digital_sum(n: int) -> int:
total = 0
while n > 0:
n, digit = divmod(n, 10)
total += digit**2
return total