Return to school: the second question-349. The intersection of two arrays

Given two arrays, write a function to calculate their intersection.

示例 1:

输入:nums1 = [1,2,2,1], nums2 = [2,2]
输出:[2]
示例 2:

输入:nums1 = [4,9,5], nums2 = [9,4,9,8,4]
输出:[9,4]
 

说明:

输出结果中的每个元素一定是唯一的。
我们可以不考虑输出结果的顺序。

answer:

This question is similar to the intersection of two arrays|| , the difference is that we only need to output one element of the intersection, that is, when the intersection has multiple 2s, output one 2. This is the interaction of sets in mathematics The repulsion is similar.
So there are also two methods:

1. Hash table:
This problem is easier to solve with a hash table. We only need to create two hash tables to store the number of numbers that appear in arrays 1, 2, and then compare the two hash tables. If the storage number of a certain number is not 0, then they have an intersection, just output the intersection number directly. (Here, my default number range is 0-10000. I thought that the test case of Likou could not be completely passed, but I finally found that it can.)

Code:

int* intersection(int* nums1, int nums1Size, int* nums2, int nums2Size, int* returnSize){
    
    
    int*temp=(int*)malloc(sizeof(int)*nums1Size);
    int hash1[10001]={
    
    0};
    int hash2[10001]={
    
    0};
    for(int i=0;i<nums1Size;i++)
    {
    
    
        hash1[nums1[i]]++;
    }
    for(int i=0;i<nums2Size;i++)
    {
    
    
        hash2[nums2[i]]++;
    }
    int index = 0;
    for(int i=0;i<=10000;i++)
    {
    
    
        if(hash1[i]==0||hash2[i]==0)
        {
    
    
            continue;
        }
        else
        {
    
    
            temp[index++]=i;
        }
    }
    *returnSize=index;
    return temp;
}

2. Sorting + double pointer:
first calculate the intersection of the two, then create an array, and select the former, that is, the repeated number only needs to be once.

Code:

/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int cmp(int*x,int*y)
{
    
    
    return *x>*y;
}
int* intersection(int* nums1, int nums1Size, int* nums2, int nums2Size, int* returnSize){
    
    
    int*temp=(int*)malloc(sizeof(int)*nums1Size);
    qsort(nums1,nums1Size,sizeof(int),cmp);
    qsort(nums2,nums2Size,sizeof(int),cmp);
    int i=0;
    int j=0;
    int index = 0;
    while(i<nums1Size&&j<nums2Size)
    {
    
    
        if(nums1[i]==nums2[j])
        {
    
    
            temp[index++]=nums1[i++];
            j++;
            continue;
        }
        else
        {
    
    
            if(nums1[i]>nums2[j])
            {
    
    
                j++;
                continue;
            }
            else
            {
    
    
                i++;
                continue;
            }
        }
    }
    int*temp2=(int*)malloc(sizeof(int)*index);
    int index2 = 0;
    for(int n=0;n<index;n++)
    {
    
    
        if(n==0)
        {
    
    
            temp2[index2++]=temp[n];
            continue;
        }
        else
        {
    
    
            if(temp[n]==temp[n-1])
            {
    
    
                continue;
            }
            else
            {
    
    
                temp2[index2++]=temp[n];
                continue;
            }
        }
    }
    *returnSize=index2;
    free(temp);
    return temp2;
}

Guess you like

Origin blog.csdn.net/xiangguang_fight/article/details/114373914