clueless coding // TODO: be smarter

LeetCode Happy Number: CPP Solution



TL;DR Code Solution


bool isHappy(int n) {

    unordered_map<int, bool> history;
    history[n] = true;
    int currentNum = 0;

    while (n != 1) {
        while (n) {
            currentNum += (n % 10 ) * (n % 10);   
            n /= 10;
        }    
        if (history[currentNum]) {
            break;
        }
        else {
            history[currentNum] = true;
        }
        n = currentNum;
        currentNum = 0;
    }

    return 1 == n;
}