C++ Solution with a lot of space


  • 0
    A
    class Solution {
    public:
        int sqSum(int num) {
            int sum = 0;
            while(num) {
                int temp = num%10;
                sum += temp*temp;
                num /= 10;
            }
            return sum;
        }
        bool isHappy(int n) {
            vector<int> record;
            int oldsum = n, newsum;
    	    record.push_back(oldsum);
            while (1) {
                newsum = sqSum(oldsum);
                if (newsum == 1)
                    return true;
                if (count(record.begin(),record.end(),newsum) > 0)
                    return false;
                record.push_back(newsum);
    	    oldsum = newsum;
            }
        }
    };
    

Log in to reply
 

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