A simple C solution [Accepted]


  • 0
    B
    /**
     * Return an array of size *returnSize.
     * Note: The returned array must be malloced, assume caller calls free().
     */
    int* intersection(int* nums1, int nums1Size, int* nums2, int nums2Size, int* returnSize) {
        int a[1000]={0};
        int b[1000]={0};
        int* result=(int*)malloc(sizeof(int)*1000);
        int temp=0;
        int temp_min=1000;
        int temp_max=0;
        for(int i=0;i<nums1Size;i++)
        {
            a[nums1[i]]++;
        }
        for(int i=0;i<nums2Size;i++)
        {
            b[nums2[i]]++;
            if(temp_min>nums2[i])
                temp_min=nums2[i];
            if(temp_max<nums2[i])
                temp_max=nums2[i];
        }
        for(int i=temp_min;i<=temp_max;i++)
        {
            if(b[i]==0)
                continue;
            if(a[i]!=0)
            {
                result[temp]=i;
                temp++;
            }
        }
        *returnSize=temp;
        return result;
    }
    

Log in to reply
 

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