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