题解 | #快乐数#
快乐数
https://www.nowcoder.com/practice/293b9ddd48444fa493dd17da0feb192d
class Solution { public://双指针 int happy(int x) { int sum=0; int m=0; while(x) { m=x%10; sum+=m*m; x=x/10; } return sum; } bool happynum(int n) { //定义快慢双指针 //快指针一次走两步,慢指针一次走一步 int slow=n,fast=happy(n); while(slow!=fast) { slow=happy(slow); fast=happy(happy(fast)); } return slow==1;//判断solw是否等于1,等于为真,返回true } };