2024-01-22 16:07:11 +08:00

20 lines
501 B
Python

# Happy Number
# Repeat the process until the number equal to 1, these are happy
# OR LOOP ENDLESS in a cycle, these are not happy
class Solution:
def isHappy(self, n: int, status: list = []) -> bool:
str_n = str(n)
total = 0
for number in str_n:
total += int(number) * int(number)
if total == 1:
return True
if str(total) in status:
return False
status.append(str(total))
self.isHappy(total, status)