LeetCode 202 Happy Number

Tag: LeetCode Posted on 2022-03-05 16:54:11 Edited on 2022-03-05 16:54:11 Views: 136

概述

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

未经允许,禁止转载,本文源站链接:https://iamazing.cn/