Ac c++ solution, simple to understand.


  • 0
    L
    class Solution {
    public:
        bool isHappy(int n) {
            vector<int> nums;
            int temp = n;
            while(temp != 1){
                if (find(nums.begin(), nums.end(), temp) != nums.end())
                    return false;
                nums.push_back(temp);
                int sum = 0;
                while(temp){
                    sum += pow((temp%10),2);
                    temp /= 10;
                }
                temp = sum;
            }
            return true;
        }
    };

Log in to reply
 

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