202. 快乐数

202. 快乐数

题目

不断的计算,使用一个set判断是否进入循环。

class Solution:
    def isHappy(self, n: int) -> bool:
        
        def jh(n):
            mysum = 0
            while n!=0:
                s = n%10
                n = n//10
                mysum += s*s
            return mysum
        
        ht = set()
        while n!=1 and n not in ht:
            ht.add(n)
            n = jh(n)
        
        return n==1

你可能感兴趣的:(202. 快乐数)