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

 

posted @ 2017-05-03 17:29  StrongYaYa  阅读(191)  评论(0编辑  收藏  举报