class Solution {
public boolean isHappy(int n) {
Set<Integer> usedInteger = new HashSet<>();
while (n != 1 && !usedInteger.contains(n)) {
usedInteger.add(n);
int sum = 0;
while (n > 0) {
int digit = n % 10;
sum += digit * digit;
n = n / 10;
}
n = sum;
}
return n == 1;
}
}
Top comments (0)
Subscribe
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Top comments (0)