TagHash

作者: inspiredhss | 来源:发表于2020-02-10 20:36 被阅读0次
# 202. Happy Number
# number === sum of the squares of its digits
# repeat the process until the number equals 1 
# 12 + 92 = 82
# 82 + 22 = 68
# 62 + 82 = 100
# 12 + 02 + 02 = 1

class Solution:
    def isHappy(self, n: int) -> bool:
        seen = set()
        while n not in seen:  #如果重复 则结束; 
            seen.add(n)   #中间数添加到集合里;
            n = sum([int(x) **2 for x in str(n)]) #中间数的平方和
        return n == 1 #判断是否为 1 
    

相关文章

网友评论

      本文标题:TagHash

      本文链接:https://www.haomeiwen.com/subject/qqdwxhtx.html