public class Solution {
public boolean isHappy(int n) {
Set<Integer> set = new HashSet<>();
while (n > 1) {
int sum = getSum(n);
if (!set.add(sum)) {
return false;
}
n = sum;
}
return true;
}
private int getSum(int n) {
int sum = 0;
while (n > 0) {
sum = sum + (n%10)*(n%10);
n = n/10;
}
return sum;
}
}
Happy Number
最新推荐文章于 2024-07-19 09:00:00 发布