[202][简单][双指针] 快乐数
最后更新于
最后更新于
class Solution:
@staticmethod
def next_number(num):
next_num = 0
while num > 0:
next_num += (num % 10) ** 2
num //= 10
return next_num
def isHappy(self, n: int) -> bool:
slow = fast = n
while fast != 1:
slow = self.next_number(slow)
fast = self.next_number(self.next_number(fast))
if slow == fast and slow != 1:
return False
return True