双指针经典 力扣202.快乐数(快慢指针)
程序员文章站
2022-05-06 21:34:42
...
class Solution {
public:
int bitSquareSum(int n) {
int sum = 0;
while(n > 0)
{
int bit = n % 10;
sum += bit * bit;
n = n / 10;
}
return sum;
}
bool isHappy(int n) {
int slow = n, fast = n;
do{
slow = bitSquareSum(slow);
fast = bitSquareSum(fast);
fast = bitSquareSum(fast);
}while(slow != fast);
return slow == 1;
}
};
上一篇: 二叉树的遍历(先序、中序、后序和层次法)
下一篇: QQ登录界面实现(JAVA)