0202.cpp (355B)
1 class Solution { 2 public: 3 bool isHappy(int n) { 4 unordered_set<int> seen; 5 int tmp; 6 7 while (n != 1 && !seen.count(n)) { 8 seen.insert(n); 9 tmp = n, n = 0; 10 do { 11 n += (tmp % 10) * (tmp % 10); 12 } while ((tmp /= 10) > 0); 13 } 14 return !seen.count(n); 15 } 16 };