1. How I sloved
I had to find out if the input number was a happy number described in the problem. So I kept splitting the input and repeated it until the sum of the squares of each number was one. If there was a cycle, the number couldn’t be the happy number. So if the sum was in cycle_list, False was returned.
2. Code
class Solution:
def isHappy(self, n: int) -> bool:
cycle_list = []
while True:
sum = 0
for i in range(len(str(n))):
sum += (int(n%10))**2
n /= 10
if sum in cycle_list:
return False
cycle_list.append(sum)
if sum == 1:
return True
n = sum
3. Result
Runtime : 20 ms(99.74%), Memory usage : 14.1 MB(99.96%)
(Runtime can be different by a system even if it is a same code.)