C Hash solution


  • 1
    G

    This uses a hash.

    #define HASHSIZE 1000
    
    /**
     * 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 *ret;
        int hash[HASHSIZE] = { 0 };
    
        if (!nums1 || !nums2)
            return NULL;
    
        *returnSize = 0;
        ret = calloc(MAX(nums1Size, nums2Size), sizeof(*nums1));
    
        for (int i = 0; i < nums1Size; ++i)
            hash[nums1[i]] = 1;
        
        for (int i = 0; i < nums2Size; ++i)
            if (hash[nums2[i]] == 1)
                hash[nums2[i]] = 2;
        
        for (int i = 0; i < HASHSIZE; ++i)
            if (hash[i] == 2)
                ret[(*returnSize)++] = i;
    
        return ret;
    }
    

Log in to reply
 

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