public class Solution {
public boolean isHappy(int n) {
HashSet<Integer> hs = new HashSet<Integer>();
// if repeat, HashSet.add() will return false
while (hs.add(n)) {
// sum the squares of n's digits
int sum = 0;
while (n > 0) {
sum += (n % 10)*(n % 10);
n /= 10;
}
if (sum == 1) return true;
n = sum;
}
return false;
}
}
Leetcode 202. Happy Number
最新推荐文章于 2022-03-21 16:21:47 发布