Simple C++ solution using to_string()


  • 0
    L
    class Solution {
    public:
        bool isHappy(int n) {
            int count=0;
            while(count<10)
            {
                ++count;
                string temp=to_string(n);
                n=0;
                for(auto i:temp)
                    n+=pow(i-'0',2);
                if(n==1) return true;
            }
            return false;
        }
    };

Log in to reply
 

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