C++ implementation


  • 0
    class Solution {
    public:
        bool isHappy(int n) {
            unordered_map<int, int> dict;
            
            while(1){
                int temp=n;
                int next=0;
                while(temp){
                    next+=(temp%10)*(temp%10);
                    temp=temp/10;
                }
                if(next==1)  return true;
                if(dict.find(n)!=dict.end())  return false;
                dict[n]=next;
                n=next;
            }
        }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.