202. 快乐数
1 //不是快乐数的数称为不快乐数(unhappy number),所有不快乐数的数位平方和计算, 2 //最後都会进入 4 → 16 → 37 → 58 → 89 → 145 → 42 → 20 → 4 的循环中。 3 class Solution 4 { 5 public: 6 bool isHappy(int n) 7 { 8 unordered_map<int,int> hash; 9 while(n != 1) 10 { 11 if(++ hash[n] == 2) return false; 12 int temp = 0; 13 while(n) 14 { 15 temp += (n % 10) * (n % 10); 16 n /= 10; 17 } 18 n = temp; 19 } 20 return true; 21 } 22 };
Mamba never out

浙公网安备 33010602011771号