C++ solution 0ms, beats 97.53%


  • 0
    T
    bool isHappy(int n) {
        int sum = 0;
        int t = n;
        if(n==1) return true;
        if(n<10 && n!=7) return false;
    
        while(n>=10)
        {
            sum = sum + (n%10)*(n%10);
            n = floor(n/10);
        }
        
        sum = sum + n*n;
        if (sum==1) return true;
        else
            return isHappy(sum);
    }

Log in to reply
 

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