边工作边刷题:70天一遍leetcode: day 38-1

happy number

错误点

  • loop中什么时候加入set?check之后update:loop invariantcheck,加入setupdate。所以一定要在loop中首先加入set
class Solution(object):
    def isHappy(self, n):
        """
        :type n: int
        :rtype: bool
        """
        uset = set()
        while n not in uset:
            uset.add(n)
            ss = 0
            while n>0:
                ss+=(n%10)*(n%10)
                n/=10
            
            if ss==1: return True
            n=ss
        return False
                
原文地址:https://www.cnblogs.com/absolute/p/5678264.html