Hash table for training camp (1)


foreword

insert image description here


Although many questions use unorder_map in C++ to solve problems, this article mainly uses C language to solve problems

1. Effective anagrams

1. Topic introduction

The topic is 242. Effective anagrams in Leekow
insert image description here

2. Ideas

Whether the two letters are anagrams, generally speaking, is to judge whether the number of occurrences of the numbers in the two strings is the same, if they are the same, it is an anagram, otherwise it is not, so how to count one What about the number of all characters in the string? We can use two arrays to map the number of occurrences of each character in the two strings. If the two arrays are exactly the same, then it can be explained that the two strings are anagrams. This method is relatively easy Think of it, we optimize it, use an array to store the number of occurrences of characters in one string, then subtract the number of occurrences of characters in another string, and finally judge whether all elements in the array are 0

3. Code

bool isAnagram(char * s, char * t){
    
    
    int res[26] = {
    
    0};
    int len1 = strlen(s);
    int len2 = strlen(t);
    for(int i = 0; i < len1; i++)
    {
    
    
        res[s[i] - 'a']++;
    }
    for(int i = 0; i < len2; i++)
    {
    
    
        res[t[i] - 'a']--;?//用第一个字符串中出现的字符的次数减去第二个字符串中字符出现的次数
    }
    for(int i = 0; i < 26; i++)
    {
    
    
        if(res[i] != 0)
            return false;
    }
    return true;
}

Second, the intersection of two arrays

1. Topic introduction

The title is the intersection of two arrays
insert image description here

2. Ideas

The idea is similar to the previous question, but at this time we will not lose the heavy elements, we need to open an additional array to save the intersection, and we need to deduplicate (note that the elements in the intersection of the answer are deduplicated )

3. Code

/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* intersection(int* nums1, int nums1Size, int* nums2, int nums2Size, int* returnSize){
    
    
    int hash[1005]={
    
    0};
    int *arr =(int *)malloc(sizeof(int)*1005);
    int j=0;
    for(int i=0;i<nums1Size;i++)
    {
    
    
        hash[nums1[i]]=1;
    }
    for(int i=0;i<nums2Size;i++)
    {
    
    
        if(hash[nums2[i]]==1)
        {
    
    
            arr[j]=nums2[i];
            j++;
            hash[nums2[i]]=0;//这一步是为了去重,只让该元素出现一次
        }
    }
    *returnSize=j;
    return arr;

}

Three, happy number

1. Topic introduction

The topic is in the number of happiness
insert image description here

2. Ideas

For this question, we need to think about 2 questions
1. How to find the sum of the squares of the digits of a number
2. How to judge that it has entered an infinite loop

1. The sum of squares extracts each digit of a number, and then squares it.
2. The infinite loop is judged by whether there will be repeated sums

3. Code

int getsum(int x)
{
    
    
    int sum=0;
    while(x)
    {
    
    
        sum+=pow(x%10,2);
        x/=10;
    }
    return sum;
}
bool isHappy(int n){
    
    
    int hash[1000]={
    
    0};
    int sum;
    sum=getsum(n);
    while(sum!=1)
    {
    
    
        if(hash[sum]!=1)
        {
    
    
            hash[sum]=1;
        }
        else
        {
    
    
            return false;
        }
        sum=getsum(sum);
    }
    return true;
}

Guess you like

Origin blog.csdn.net/Ruiren_/article/details/130031787