/**
* @Description 这个有意思了,虽然是一个卑微的简单题
* 但其实使用哈西计数法有隐藏的内存溢出问题
* 递归调用也会存在栈溢出问题
* 所以最优解是快慢指针法
* @Date 2022/1/12 12:26 上午
* @Author wuqichuan@zuoyebang.com
**/
public class Solution {
public int getNext(int n){
int sum = 0;
while (n > 0) {
int d = n % 10;
n = n / 10;
sum += d * d;
}
return sum;
}
public boolean isHappy(int n) {
int slow = n;
int fast = getNext(n);
while (fast != 1 && fast != slow){
slow = getNext(slow);
fast = getNext(fast);
fast = getNext(fast);
}
return fast == 1;
}
}