题目描述:

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

 

相关文章:

  • 2022-12-23
  • 2021-12-26
  • 2021-07-30
  • 2021-08-09
  • 2021-10-20
  • 2022-12-23
猜你喜欢
  • 2021-07-26
  • 2021-11-27
  • 2021-06-05
  • 2021-07-27
  • 2022-12-23
相关资源
相似解决方案