class Solution {
public:
int happy(int num){
int a = 0;
while (num!=0) {
a += int(pow(num%10, 2));
num/=10;
}
return a;
}
bool isHappy(int n) {
int slow, fast;
slow = fast = n;
do {
slow = happy(slow);
fast = happy(fast);
fast = happy(fast);
} while(slow != fast);
return slow==1?1:0;
}
};
Happy Number
Copyright © 2017 Powered by LZH, Theme used GitHub CSS.