题解 | #快乐数#
快乐数
http://www.nowcoder.com/practice/293b9ddd48444fa493dd17da0feb192d
import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param n int整型
* @return bool布尔型
*/
public boolean happynum (int n) {
// write code here
int slow=n;int fast=reserve(n);
while(slow!=fast){
slow=reserve(slow);
fast=reserve(reserve(fast));
}
return slow==1;
}
private int reserve(int n){
int sum=0;
while(n>0){
int tmp=n%10;
sum=tmp*tmp+sum;
n=n/10;
}
return sum;
}
}