202. Happy Number
https://leetcode.com/problems/happy-number/
Input: 19
Output: true
Explanation:
1^2 + 9^2 = 82
8^2 + 2^2 = 68
6^2 + 8^2 = 100
1^2 + 0^2 + 0^2 = 1Thoughts
Code
class Solution:
def isHappy(self, n: int) -> bool:
v = set()
while n not in v:
if n == 1:
return True
v.add(n)
n = sum(int(c) ** 2 for c in str(n))
return FalseAnalysis
Ver.2
Last updated