Easy to Understand c++


  • 0
    L
    class Solution {
    public:
        int help(int n){
            int res = 0;
            while(n){
                res += (n%10)*(n%10);
                n /= 10;
            }
            return res;
        }
        bool isHappy(int n) {
            int slow = n;
            int fast = n;
            do{
                slow = help(slow);
                fast = help(help(fast));
            }while(fast != slow);
            return 1 == fast;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.