202. Happy Number
用HashSet存储已经出现过的数字,若重复出现肯定就不行了。

public boolean isHappy(int n) {
    Set<Integer> inLoop =
new HashSet<Integer>();
   
int squareSum,remain;
while (inLoop.add(n)) {
squareSum =
0;
while (n > 0) {
    remain = n%
10;
squareSum += remain*remain;
n /=
10;
}
if (squareSum == 1)
return true;
else
n = squareSum;

}
return false;

}

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-26
  • 2022-12-23
  • 2021-11-05
猜你喜欢
  • 2021-07-26
  • 2021-11-27
  • 2021-06-05
  • 2021-07-27
  • 2022-02-04
相关资源
相似解决方案