Accepted 0ms C++ with recursion. Code comments included.


  • -3
    P
    class Solution {
    public:
        bool isHappy(int n) {
            
        int sum = 0;
        int digit;
    
        // Return true since it's 1
    	if(n == 1)
    		return true;
    
        // Loop through each digit and add the square of it to the sum.
        // Then drop that digit off n until we have done all the digits.
        do
        {
            digit = n % 10;
            sum += (digit * digit);
            n /= 10;
        }while(n > 0);
    
        // If the sum is 1, we're done and it's happy.
        // If the sum is > 9, then we need to recurse since it's multiple digits.
        // If the sum is < 10 and not 1, it's not happy.
        if(sum == 1)
            return true;
    	else if(sum > 9)
    		return isHappy(sum);
    	else
    		return false;
      }
    };

  • 2
    L

    Since 7 is happy, your solution does not work for all inputs. For example, n = 2111 would give sum = 7, which your code would mark as unhappy. However, 7 -> 49 -> 97 -> 130 -> 10 -> 1.


  • 0
    P

    Thanks for catching that. I believe all that needs to be done is change the line:

    if(sum == 1)

    to

    if(sum == 1 || sum == 7)

    Here is the revised code:

    class Solution {
    public:
    bool isHappy(int n) {

    int sum = 0;
    int digit;
    
    // Return true since it's 1
    if(n == 1)
    	return true;
    
    // Loop through each digit and add the square of it to the sum.
    // Then drop that digit off n until we have done all the digits.
    do
    {
        digit = n % 10;
        sum += (digit * digit);
        n /= 10;
    }while(n > 0);
    
    // If the sum is 1, we're done and it's happy.
    // If the sum is > 9, then we need to recurse since it's multiple digits.
    // If the sum is < 10 and not 1, it's not happy.
    if(sum == 1 || sum == 7)
        return true;
    else if(sum > 9)
    	return isHappy(sum);
    else
        return false;
    

    }
    };


Log in to reply
 

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