题目

题解


对于第三中情况
class Solution {public:bool isHappy(int n) {unordered_set<int> seen;while (n != 1 && (seen.find(n)==seen.end())) {seen.insert(n);n = getNext(n);}return n == 1;}int getNext(int n) {int totalSum = 0;int d;while (n > 0) {d = n % 10;n = n / 10;totalSum += d * d;}return totalSum;}};
