202. 快乐数(算法快慢指针)


其中快慢指针比较常用 慢指针走一下快指针走两下,不能同时走一下,同时走两个的距离不变 相当于是 a=b 左右两边同时加实数等号依旧成立

bool isHappy(int n){
   int p=n,r=Signal(n);
    while(p!=r){
        p=Signal(p);
        r=Signal(r);
        r=Signal(r);
        
       
    }
    if(p==1)
    return true;
    return false;
}
int Signal(int t){
    int num=0;
    while(t){
            num+=pow(t%10,2);
            t/=10;
        }
        return num;
}
暴力

bool isHappy(int n){
    int ans=10000;
    while(ans--){
        int t=n;
        int num=0;
        while(t){
            num+=pow(t%10,2);
            t/=10;
        }
        n=num;
        if(n==1)
    return true;
    }
    
    return false;
}

[https://leetcode-cn.com/problems/happy-number/]