[Algorithms] LeetCode 202. 快乐数
Categories Algorithms HashTable
Tags
来源:代码随想录
用unordered_set保存出现过的结果
四刷:先插入再更新。
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> us;
while (us.find(n) == us.end() && n != 1) {
us.insert(n);
n = getSum(n);
}
if (n == 1) {
return true;
}
return false;
}
};
Page views: Loading... · Visitors: Loading...
Except where otherwise noted, original content on this site is dedicated to the public domain under CC0 1.0.
Powered by Hexo & Theme mdsuper
沪ICP备2026040813号