Skip to content

Latest commit

 

History

History
17 lines (17 loc) · 388 Bytes

202. Happy Number.md

File metadata and controls

17 lines (17 loc) · 388 Bytes
class Solution {
public:
    bool isHappy(int n) {
        unordered_set<int> s;
        while (true) {
            if (n == 1) return true;
            if (s.count(n)) return false;
            s.insert(n);
            string s = to_string(n);
            n = 0;
            for (int i = 0; i < s.size(); ++i) 
                n += (s[i] - '0') * (s[i] - '0');
        }
    }
};