Happy number
Input: 19
Output: true
Explanation:
12 + 92 = 82
82 + 22 = 68
62 + 82 = 100
12 + 02 + 02 = 1class Solution:
def isHappy(self, n: int) -> bool:
def getSum(i):
res = 0
while i > 0:
res+= (i%10)**2
i //= 10
return res
slow=fast=n
while True:
slow= getSum(slow)
fast = getSum(fast)
fast = getSum(fast)
if fast == 1:
return True
if slow == fast:
break
return False
Last updated