leetcode 202. Happy Number (Hash Table)

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

"Happy Number" is defined as:

For a positive integer, each time the number is replaced by the sum of the squares of the digits in each position.
Then repeat this process until the number becomes 1, or it may be an infinite loop but it never becomes less than 1.
If the result of this process is 1, then the number is a happy number.
Returns true if n is a happy number; otherwise, returns false.

Example 1:
Input: n = 19
Output: true
Explanation:
1 2 + 9 2 = 82
8 2 + 2 2 = 68
6 2 + 8 2 = 100
1 2 + 0 2 + 0 2 = 1

Example 2:
Input: n = 2
Output: false

hint:

1 <= n <= 231 - 1

Quote it because it's true.

insert image description here

The code that comes to mind at first is as follows:

The idea is: use an array to count whether there is a cycle in it. If a number appears once, add one to the corresponding value in the array. If a number is a happy number, there will be no cycle, otherwise it will definitely enter a cycle.
Therefore, if there is a number greater than 1 in the array count, it means that the loop has entered, and it is not a happy number, and returns false.

class Solution {
    
    
public:
    bool isHappy(int n) {
    
    
        int a[100001]={
    
    0};
        memset(a,0,sizeof(a));
        int he=0,m=n;
        while(1){
    
    
            he=0;
            while(m){
    
    
                he+=((m%10)*(m%10));
                m/=10;
            }
            if(he==1)
                return true;
            a[he]++;
            if(a[he]>1)  //如果一个数出现了不止一次,说明有循环,返回false就可以了
                return false;
            m=he;
        }
        return false;
    }
};


Because I have recently learned the hash table, and the classification of this question is in the hash table. The a array of the first code is used for counting, so the hash table can also be used in this way, because the hash table is not allowed There is the same key value, so we store the calculated data as the key value in the hash table every time, and then look for the same key value in the hash table, if there is, it means entering the loop, otherwise no.

class Solution {
    
    
public:
    bool isHappy(int n) {
    
    
        unordered_map<int,int> hashtable;
        hashtable.insert(make_pair(n,0));//将n本身存入哈希表中,也是需要判断的,value值是多少都没关系,根本用不到
        int he=0;
        while(1){
    
    
            he=0;
            while(n){
    
    
                he+=((n%10)*(n%10));
                n/=10;
            }
            if(he==1)
                return true;
            if(hashtable.find(he)!=hashtable.end())//如果找到了一个重复的,说明其中有循环,就不是快乐数
                return false;
            else//放入哈希表中为下一次判断准备
                hashtable.insert(make_pair(he,0));
            n=he;
        }
        return false;
    }
};


After I read the comments, I thought it was right, and I simplified the code a bit, so that as long as any of these numbers appear, I don't need to continue to count.

insert image description here

class Solution {
    
    
public:
    bool isHappy(int n) {
    
    
        int he=0;
        while(1){
    
    
            he=0;
            while(n){
    
    
                he+=((n%10)*(n%10));
                n/=10;
            }
            if(he==1)
                return true;
            if(he==4||he==16||he==37||he==58||he==89||he==145||he==42||he==20)
                return false;//特殊判断,如果不是快乐数一定会进入这个循环
            n=he;
        }
        return false;
    }
};


After reading the comments, I suddenly found another thing, that is, the question says that the absolute value of the difference is less than k, but if the difference is negative, it must be less than k, and the question says that k is greater than or equal to 0, then if the difference It is less than k when it is a positive number, and it must be less than a negative number, so the above code can pass without adding the abs() function.



Guess you like

Origin blog.csdn.net/xiatutut/article/details/127326446