题目描述
点这里
思路分析
变相的链表快慢指针问题,问最终环内的点是否为1
具体参考这个题
代码实现
class Solution {
public:
int f(int x){
int res=0;
while(x){
res+=(x%10)*(x%10);
x/=10;
}
return res;
}
bool isHappy(int n) {
int fast=f(n),slow=n;
while(fast!=slow){
fast=f(f(fast));
slow=f(slow);
}
return fast==1;
}
};