首页 > 其他分享 >LeetCode 202 快乐数

LeetCode 202 快乐数

时间:2022-09-05 17:23:42浏览次数:53  
标签:10 202 set int sum 快乐 return LeetCode

class Solution {
public:
    int getSum(int n) {
        int sum = 0;
        while (n) {
            sum += (n % 10) * (n % 10);
            n /= 10;
        }

        return sum;
    }

    bool isHappy(int n) {
        unordered_set<int> set;
        while (1) {
            int sum = getSum(n);
            if (sum == 1) return true;

            if (set.find(sum) != set.end()) return false;
            else set.insert(sum);

            n = sum;

        }
    }
};

标签:10,202,set,int,sum,快乐,return,LeetCode
From: https://www.cnblogs.com/hjy94wo/p/16658896.html

相关文章