视频讲解:力扣经典算法面试题:快乐数(力扣 202)_哔哩哔哩_bilibili
文字讲解:代码随想录
力扣地址:. - 力扣(LeetCode)
记录:这道题很巧妙 将快乐数转换为链表是否有环的问题了 代码直接上了 理解了就秒了
func isHappy(n int) bool {
slow := n
fast := next(n)
for slow != fast {
slow = next(slow)
fast = next(next(fast))
}
return fast == 1
}
func next(n int) int {
sum := 0
for n>0{
sum += (n%10) * (n%10)
n = n/10
}
return sum
}