202. Happy Number
Easy
Input: n = 19
Output: true
Explanation:
12 + 92 = 82
82 + 22 = 68
62 + 82 = 100
12 + 02 + 02 = 1Last updated
Input: n = 19
Output: true
Explanation:
12 + 92 = 82
82 + 22 = 68
62 + 82 = 100
12 + 02 + 02 = 1Last updated
Input: n = 2
Output: falseclass Solution:
def isHappy(self, n: int) -> bool:
d = {}
num = n
while num != 1:
if num not in d:
d[num] = 1
else:
return False
sum_ = 0
for digit in str(num):
sum_ += pow(int(digit), 2)
num = sum_
return True