TagHash

2020-02-10  本文已影响0人  inspiredhss
# 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 
    
上一篇 下一篇

猜你喜欢

热点阅读