#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
#
# @param n int整型
# @return bool布尔型
#
import bisect
class Solution:
def happynum(self , n: int) -> bool:
# write code here
# #用于存储过程中出现的数,重复出现则证明陷入循环
# lst = list()
# while(n!=1):
# #如果n存在于数组中,则已经陷入循环不可能为快乐数
# idx=bisect.bisect_left(lst,n) #防止超时
# if idx>0 and idx<len(lst) and lst[idx]==n:
# return False
# else:
# #没出现过,加入数组中
# lst.append(n)
# #用于求各位平方和
# sum = 0
# temp = n
# while(temp!=0):
# sum+=(temp%10)**2
# temp=temp//10
# n=sum
# #n==1时退出循环,返回true
# return True
#集合法
S =set()
while n not in S:
if n==1:
return True
else:
S.add(n)
n = sum(int(digit)**2 for digit in str(n))
return False