【leetcode】202. Happy Number

来源:互联网 发布:人工智能的就业前景 编辑:程序博客网 时间:2024/05/02 10:19
@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;    }};
0 0