@requires_authorization
@author johnsondu
@create_time 2015.7.26 9:48
@url [Happy Number](https://leetcode.com/problems/happy-number/)
/************************
 * @description: simple.
 * @time_complexity:  O(n)
 * @space_complexity: O(n)
 ************************/
class Solution {
public:
    bool isHappy(int n) {
        bool flag = false;
        map<int, int> mp;
        mp[n] = true;
        while(1) {
            int cur = 0;
            while(n) {
                cur += (n % 10) * (n % 10);
                n = n / 10;
            }
            if(cur == 1) {
                flag = true;
                break;
            }
            if(mp[cur]) break;
            mp[cur] = true;
            n = cur;
        }
        return flag;
    }
};版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/zone_programming/article/details/47065495