E-202. Happy Number

快慢指针判断有环链表的起点证明
# 0 O(?), O(?)
class Solution:
    def isHappy(self, n: int) -> bool:
        seen = set()
        while n not in seen:
            seen.add(n)
            n = sum(int(i)**2 for i in str(n))
        print(seen)
        return n == 1

Last updated

Was this helpful?