[Leetcode] 202. 快乐数

2020-04-30  本文已影响0人  丶噗噗噗噗噗

202. 快乐数

来源: 202. 快乐数

1. 解题思路

设置集合查找是否已出现过

2. 代码

class Solution:
    def change(self, n):
        res=0
        while n/10>0:
            res+=(n%10)*(n%10)
            n=n//10
        return res

    def isHappy(self, n: int) -> bool:
        nums = set()
        while n not in nums:
            if n == 1:
                return True
            nums.add(n)
            n = self.change(n)
        return False
上一篇下一篇

猜你喜欢

热点阅读