class Solution {
public boolean isHappy(int n) {
Set<Integer> set = new HashSet<>();
while(n != 1 && !set.contains(n)) {
set.add(n);
n = getNext(n);
}
return n == 1;
}
public int getNext(int n) {
int ans = 0;
while(n != 0) {
int d = n % 10;
n = n / 10;
ans += d * d;
}
return ans;
}
}