题目描述:
主要思路:
这个题的主要问题在于判断循环,之前判断循环一直用的是哈希表,现在学习到了一种新的东西是快慢指针。
class Solution {
public:
int find(int x)
{
int ans=0;
while(x)
{
ans+=(x%10)*(x%10);
x/=10;
}
return ans;
}
bool isHappy(int n) {
int slow=n,fast=n;
do{
slow=find(slow);
fast=find(fast);
fast=find(fast);
}while(slow!=fast);
return slow==1;
}
};