leetcode 202. Happy Number
题目描述:
用STL中的unorder_map<int,bool> 键值对 哈希map
class Solution { public: bool isHappy(int n) { unordered_map<int,bool> m; int sum = Happy(n); while(sum != 1){ if(m[sum] == true) return false; //出现了循环 else m[sum] = true; sum = Happy(sum); } return true; } int Happy(int n){ int ret = 0; while(n){ ret += pow((n%10),2); n = n/10; } return ret; } };
幸运之神的降临,往往只是因为你多看了一眼,多想了一下,多走了一步。