leecode

leecode202,快乐数,环形链表法,带循环的规律都可以考虑用环形链表

class Solution:
    def isHappy(self, n: int) -> bool:  
        def get_next(number):
            total_sum = 0
            while number!= 0:
                tmp = number%10
                number //= 10
                total_sum += tmp**2
            return total_sum


        s = n
        f = get_next(n)
        while f != 1 and f != s:
            s = get_next(s)
            f = get_next(get_next(f))
        return f == 1

leecode258,各位相加,数根,找数学规律

class Solution:
    def addDigits(self, num: int) -> int:
        return (num-1)%9+1 if num else 0

你可能感兴趣的:(链表,数据结构)