https://leetcode.cn/problems/happy-number/description/
思路
无限循环,那么也就是说求和的过程中,sum会重复出现
code
class Solution {
public:
int getNum(int n){
int sum=0;
int m;
while(n){
m=n%10;
sum+=m*m;
n/=10;
}
return sum;
}
bool isHappy(int n) {
unordered_set<int> res;
int sum;
while(true){
sum=getNum(n);
if(sum==1)
return true;
if(res.find(sum)!=res.end()){
return false;
}else{
res.insert(sum);
}
n=sum;
}
}
};