美文网首页
[Leetcode] 202. 快乐数

[Leetcode] 202. 快乐数

作者: 丶噗噗噗噗噗 | 来源:发表于2020-04-30 13:55 被阅读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
    

    相关文章

      网友评论

          本文标题:[Leetcode] 202. 快乐数

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