Firstly, I calculate the result of number 1 - 9 by hand, find that only 1 and 7 is happy number, so the solution is very simple:

code :

class Solution {

public:

bool isHappy(int n) {

int num;

int t;

while (n >= 10)

{

num = 0;

while (n)

{

t = n % 10;

num += t * t;

n /= 10;

}

n = num;

}

return n == 1 || n == 7;

}

};