public class Solution {
public boolean isHappy(int n) {
HashSet<Integer> unhappy = new HashSet<Integer>();
while (n != 1) {
if (unhappy.contains(n)) {
return false;
}
unhappy.add(n);
n = next(n);
}
return true;
}
private int next (int n) {
int next = 0;
while (n != 0) {
next += (n % 10) * (n % 10);
n = n / 10;
}
return next;
}
}
Happy Number
最新推荐文章于 2020-05-01 00:21:11 发布