A simple C++ solutinon 8ms


  • 0
    class Solution{
    
    public:
    bool isHappy(int n) {
        vector<int>v;
        n=Next(n);
        v.push_back(n);
        while(n!=1&&n!=4){
            n=Next(n);
            v.push_back(n);
        }
        if(find(v.begin(),v.end(),1) != v.end())
            return true;
        return false;
    }
    private:
    int Next(int n){
        int res=0;
        while(n){
            res += pow(n%10,2);
            n /= 10;
        }
        return res;
    }
    

    };


  • 0
    B
    This post is deleted!

Log in to reply
 

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