class Solution {
public:
int getNext(int val) {
int ret = 0;
while (val > 0) {
int temp = val % 10;
ret = temp * temp + ret;
val /= 10;
}
return ret;
}
bool isHappy(int n) {
int fast = getNext(n), slow = n;
while (fast != 1 && fast != slow) {
fast = getNext(getNext(fast));
slow = getNext(slow);
}
return fast == 1;
}
};