完整教程:【算法】双指针(三)[快慢指针]-快乐数

目录

一、题目介

二、快慢指针原理

三、提交代


一、题目介绍

202. 快乐数 - 力扣(LeetCode)


二、快慢指针原理

相对靠近每步地自互跑到圈上快指针往回追赶


三、提交代码

public int bitSum(int n) {
int sum = 0;
while (n != 0) {
int tmp = n % 10;
sum += tmp * tmp;
n /= 10;
}
return sum;
}
public boolean isHappy(int n) {
int slow = n;
int fast = bitSum(n);
while (slow != fast) {
slow = bitSum(slow);
fast = bitSum(bitSum(fast));
}
return fast == 1;
}

posted on 2025-09-19 22:32  ljbguanli  阅读(11)  评论(0)    收藏  举报