Leetcode 202 Solution

This article provides solution to leetcode question 202 (happy-number).

https://leetcode.com/problems/happy-number

Solution

class Solution {
public:
    bool isHappy(int n) {
        set<int> a;
        
        while (a.find(n) == a.end())
        {
            a.insert(n);

            int next = 0;
            
            while (n)
            {
                next += (n % 10) * (n % 10);
                n /= 10;
            }
            
            if (next == 1)
                return true;
            
            n = next;
        }
        
        return false;
    }
};