Happy Number
Description
Code
class Solution {
public boolean isHappy(int n) {
Set<Integer> set = new HashSet<>();
while (n != 1) {
if (set.contains(n)) return false;
set.add(n);
n = digitSquareSum(n);
}
return true;
}
public int digitSquareSum(int n) {
int result = 0;
while (n != 0) {
int mod = n % 10;
n /= 10;
result += mod*mod;
}
return result;
}
}Last updated