0ms C++ recursion solution


  • 0
    S
    class Solution {
    public:
        bool isHappy(int n) {
            int sum = 0;
            while(n > 0)
            {
                int x = n % 10;
                sum += x*x;
                n /= 10;
            }
            if(sum == 4)
            {
                return false;
            }
            else if(sum == 1)
            {
                return true;
            }
            else
            {
                return isHappy(sum);
            }
        }
    };
    ···
    ···>

  • 0

    the question is,how can you confirm it will return false when n == 4 .


Log in to reply
 

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