I think the important part is how to end a potential infinite loop. Some suggest using Set, it is a straight forward solution, but I found it will increase the time cost. Therefore, I use another condition to detect an infinite loop, which is a bit tricky though.

```
public class Solution {
public boolean isHappy(int n) {
if ( n < 5) {
if (1 == n)
return true;
else
return false;
}
int sum = 0;
while ( n >= 10) {
sum += (n % 10) * (n % 10);
n = n / 10;
}
sum += n * n;
return isHappy(sum);
}
}
```