LeetCode 202 Happy Number
概述
https://leetcode.com/problems/happy-number/
set + 递归
class Solution {
public:
unordered_set<int> s;
bool isHappy(int n) {
int a = 0;
while (n != 0) {
a += pow(n % 10, 2);
n /= 10;
}
if (a == 1) return true;
if (s.count(a)) return false;
s.insert(a);
return isHappy(a);
}
};
Links: leetcode-202-happy-number