LeetCode 202 Happy Number

判断是否是快乐数字

标签:LeetCode发布于:编辑于:浏览量:1597

概述

https://leetcode.com/problems/happy-number/

set + 递归

class Solution {
public:
    unordered_set<int> s;
    bool isHappy(int n) {
        int a = 0;
        while (n != 0) {
            a += pow(n % 10, 2);
            n /= 10;
        }
        if (a == 1) return true;
        if (s.count(a)) return false;
        s.insert(a);
        return isHappy(a);
    }
};