1. /**
    2. * @Description 这个有意思了,虽然是一个卑微的简单题
    3. * 但其实使用哈西计数法有隐藏的内存溢出问题
    4. * 递归调用也会存在栈溢出问题
    5. * 所以最优解是快慢指针法
    6. * @Date 2022/1/12 12:26 上午
    7. * @Author wuqichuan@zuoyebang.com
    8. **/
    9. public class Solution {
    10. public int getNext(int n){
    11. int sum = 0;
    12. while (n > 0) {
    13. int d = n % 10;
    14. n = n / 10;
    15. sum += d * d;
    16. }
    17. return sum;
    18. }
    19. public boolean isHappy(int n) {
    20. int slow = n;
    21. int fast = getNext(n);
    22. while (fast != 1 && fast != slow){
    23. slow = getNext(slow);
    24. fast = getNext(fast);
    25. fast = getNext(fast);
    26. }
    27. return fast == 1;
    28. }
    29. }