bool isHappy(int n) {
int res;
while (1)
{
res = 0;
for (; n; n/=10) res += (n % 10)*(n % 10);
n = res;
if (n == 1) return true; // n will always come to 1 or 4 ^^
else if (n == 4) return false;
}
}
Very short C solution. 0ms

I was planning to write some mathematical explanation, but then I found there are already some in this forum.
Maybe you can refer to
https://discuss.leetcode.com/topic/30520/explanationofwhythosepostedalgorithmsaremathematicallyvalid
or
https://discuss.leetcode.com/topic/42746/allyouneedtoknowabouttestinghappynumberIt shows there are only two cases for n when the loop goes on