力扣面试150 快乐数 循环链表找环 链表抽象 哈希
Problem: 202. 快乐数
👩🏫 参考题解
Code
public class Solution {
public int squareSum(int n) {
int sum = 0;
while(n > 0){
int digit = n % 10;
sum += digit * digit;
n /= 10;
}
return sum;
}
public boolean isHappy(int n) {
int slow = n, fast = squareSum(n);
while (slow != fast){
slow = squareSum(slow);
fast = squareSum(squareSum(fast));
};
return slow == 1;
}
}