LeetCode 202 快乐数
作者:互联网
class Solution {
public:
int getSum(int n) {
int sum = 0;
while (n) {
sum += (n % 10) * (n % 10);
n /= 10;
}
return sum;
}
bool isHappy(int n) {
unordered_set<int> set;
while (1) {
int sum = getSum(n);
if (sum == 1) return true;
if (set.find(sum) != set.end()) return false;
else set.insert(sum);
n = sum;
}
}
};
标签:10,202,set,int,sum,while,快乐,return,LeetCode 来源: https://www.cnblogs.com/hjy94wo/p/16658896.html