202. Happy Number 發表於 2023-02-13 | 分類於 leetcode problemsolution123456789101112131415161718192021class Solution {public: bool isHappy(int n) { // 19 -> 82 -> 68 -> 100 -> 1 // 2 -> 4 -> 16 -> 37 -> 58 -> 89 -> 145 -> 42 -> 20 -> 4 int happy = n; unordered_set<int> s({happy}); while(happy!=1){ int m=0; while(happy){ m+=(happy%10)*(happy%10); happy/=10; } happy = m; if(s.find(happy)!=s.end()) return false; s.insert(happy); } return true; }}; analysis