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 };

 

posted @ 2020-04-04 15:18  Jinxiaobo0509  阅读(113)  评论(0)    收藏  举报