```
class Solution {
public:
bool isHappy(int n) {
int sum = 0, num = 0;
set<int> s;
s.insert(n);
while (1)
{
sum = 0;
while (n > 0)
{
num = n % 10;
sum += num * num;
n /= 10;
}
if (sum == 1) return true;
else
n = sum;
if (s.find(n) != s.end())
return false;
else
s.insert(n);
}
}
};
```