import java.util.*;
public class Solution {
public int powSum(int n){
int ans = 0;
while(n>0){
int t = n%10;
ans += t*t;
n/=10;
}
return ans;
}
public boolean happynum (int n) {
//一般来说数位的平方和,会比原来的数小
//有限的范围计算无数次,必然出现重复的数
Set<Integer> set = new HashSet<>();
int cur = powSum(n);
while(cur!=1&&!set.contains(cur)){
set.add(cur);
cur = powSum(cur);
}
if(cur==1)return true;
else return false;
}
}

京公网安备 11010502036488号