class Solution
{
public:
    bool isHappy(int n)
    {
        map<int, int> m;
        while (n != 1 && m.find(n) == m.end())
        {
            m.insert(pair<int, int>(n, 0));
            int s = 0;
            while (n)
            {
                s += pow((n % 10), 2);
                n /= 10;
            }
            n = s;
        }
        if (n == 1)
            return true;
        else
            return false;
    }
};
