#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
#
# @param n int整型
# @return bool布尔型
#
class Solution:
def happynum(self , n: int) -> bool:
# write code here
a = [0,1,4,9,16,25,36,49,64,81]
ans = []
temp = n
while True:
if temp in ans:
return False
if str(temp)==('1'+'0'*(len(str(temp))-1)):
return True
else :
ans.append(temp)
fu= str(temp)
total=0
for x in fu:
total+=a[int(x)]
temp=total
退出循环的条件是目前值对应的数已经求过了,则进入循环不可能出现新值。return False

京公网安备 11010502036488号