LeetCode #202 Happy Number Solution & Explanation

LeetCode Problem

Write an algorithm to determine if a number n is happy.

happy number is a number defined by the following process:

  • Starting with any positive integer, replace the number by the sum of the squares of its digits.
  • Repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1.
  • Those numbers for which this process ends in 1 are happy.

Return true if n is a happy number, and false if not.


Solution

In the first solution, we use an array to store the number that have used ( numbers that aren’t Happy)

And in the next iteration, if any numbers repeat again, we can return False.

C# Solution

Solution1

public class Solution {
    public bool IsHappy(int n) {
        
        List<int>used = new List<int>();

        while(n>0)
        {
            int tmp = 0;
            while(n>0)
            {
                int i = n%10;

                tmp += i*i;
                n = n/10;
            }

            if(used.Contains(tmp))
            {
                return false;
            }
            else
            {
                used.Add(tmp);    
            }
            

            if(tmp==1)
            {
                return true;
            }

            n = tmp;

        }
        
        return false;
    }
}

Java Solution

Solution1

class Solution {
    public boolean isHappy(int n) {
        List<Integer>used = new ArrayList<Integer>();

        while(n>0)
        {
            int tmp = 0;
            while(n>0)
            {
                int i = n%10;

                tmp += i*i;
                n = n/10;
            }

            if(used.contains(tmp))
            {
                return false;
            }
            else
            {
                used.add(tmp);    
            }
            

            if(tmp==1)
            {
                return true;
            }

            n = tmp;

        }
        
        return false; 
    }
}

Runtime : 1ms、1ms、3ms

Python3 Solution

Solution1

class Solution:
    def isHappy(self, n: int) -> bool:
        used = []

        while(n>0):
            tmp = 0
            while(n>0):
                i = n%10

                tmp += i*i
                n = n//10

            if(tmp in used):
                return False
            else:
                used.append(tmp)

            if(tmp==1):
                return True
            
            n = tmp

        return False

Python’s // give result to integer

JavaScript Solution

Solution1

/**
 * @param {number} n
 * @return {boolean}
 */
var isHappy = function(n) {
    let used = new Set();

    while(n>0)
    {
        let tmp = 0;
        while(n>0)
        {
            let i = n%10;

            tmp += i*i;
            n = Math.floor(n/10);
        }

        if(used.has(tmp))
        {
            return false;
        }
        else
        {
            used.add(tmp);    
        }
        

        if(tmp==1)
        {
            return true;
        }

        n = tmp;

    }
    
    return false; 
};

Runtime : 69ms、69ms、66ms Faster than 80~90%

You can also use array if you want, but remember use array’s includes() to replace set’s has()


Conclusion

🧡If my solution helps, that is my honor!

🧡You can support me by clicking some ad, and that will make me Happy

If you got any problem about the explanation, please feel free to let me know

Cover Photo – Photo by Count Chris on Unsplash

The problem link : Happy Number – LeetCode

Random post

Leave a Reply

Your email address will not be published. Required fields are marked *