llllmz

导航

202. 快乐数

预计再过半个月就可以写项目了,不知道像我这样的彩笔,能不能驾驭得住项目难度,希望可以随便拿捏。

class Solution {
public:
    bool isHappy(int n) {
        unordered_set<int> unset;
        while(1){
            int sum = getNextNum(n);
            if(sum == 1) return true;
            if(unset.find(sum) != unset.end()){
                return false;
            }else{
                unset.insert(sum);
            }
            n = sum;
        }
    }
private:
    int getNextNum(int n){
        int ret = 0;
        while(n != 0){
            int temp = n % 10;
            ret += pow(temp, 2);
            n /= 10;
        }
        return ret;
    }
};

 

posted on 2024-10-06 20:49  神奇的萝卜丝  阅读(18)  评论(0)    收藏  举报