[LeetCode] 350.两个数组的交集 II(Easy)C语言题解

题目

  • 给定两个数组,编写一个函数来计算它们的交集。

示例

①示例1

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

②示例2

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

说明

输出结果中每个元素出现的次数,应与元素在两个数组中出现的次数一致(即不用去重)。
我们可以不考虑输出结果的顺序。

①数据范围(自测)

  • -2147483648 ~ 2147483647
    | 与 349. 两个数组的交集 — 力扣网 相比,数据范围太大且单组数据的数据差也很大,无法用数组模拟的哈希表求解。

②相关话题

  • 哈希表
  • 排序
  • 双指针
  • 二分查找

③相似题目

  • 349. 两个数组的交集 — 力扣网
  • 349. Intersection of Two Arrays — leetcode
  • 1002. 查找常用字符 — 力扣网
  • 1002. Find Common Characters — leetcode

④题目地址

  • 350. 两个数组的交集 II — 力扣网
  • 350. Intersection of Two Arrays II — leetcode

解题方法

①哈希表

  • 因为数据范围太大,无法用数组模拟的哈希表做,但是可以使用 uthash
  • uthash 是一个用 C 语言编写的开源库,使用宏实现了哈希表的增删改查等功能。
  • 注意:使用哈希表时,要分别记录数组中每个元素出现的次数。
  • 时间复杂度:O(N)。
  • 空间复杂度:O(N)。

②排序

  • 先使用快排函数排序两个数组,再通过双指针查找交集。
  • 时间复杂度:O(Nlogn)。
  • 空间复杂度:O(1)。

代码详解

  • uthash
int* intersect(int* nums1, int nums1Size, int* nums2, int nums2Size, int* returnSize) {    
    struct hash {
        int value;
        int count;
        // makes this structure hashable.
        UT_hash_handle hh; 
    };
    
    struct hash *hashTable = NULL;
    int* result = malloc(sizeof(int)*nums1Size);
    
    for (int i = 0; i < nums1Size; i++) {
        struct hash *h;
        // 查找值。
        HASH_FIND_INT(hashTable, nums1+i, h);
        
        if (!h) {
            h = malloc(sizeof(struct hash));
            h->value = nums1[i];
            h->count = 1;
            // 若值不存在则添加,并令出现次数==1。
            HASH_ADD_INT(hashTable, value, h);
        }
        // 若值已存在则出现次数+1。
        else
            h->count++;
    }
    for (int i = 0; i < nums2Size; i++) {
        struct hash *h;
        HASH_FIND_INT(hashTable, nums2+i, h);
        
        if (h && h->count > 0) {
            result[(*returnSize)++] = h->value;
            h->count--;
        }
    }
    
    return result;
}

  • 排序 + 双指针查找
// 快排函数(升序)。
int compare(const void* a, const void* b) {
	/*
	 bug: 若是将 > 改为 -,可能溢出整型范围。
	如测试数据: [-2147483648,1],在排序时会导致异常: runtime error: signed integer overflow: -2147483648 - 1 cannot be represented in type 'int' (solution.c)
	 */
    return *(int*)a > *(int*)b; 
}

int* intersect(int* nums1, int nums1Size, int* nums2, int nums2Size, int* returnSize) {    
    // 排序。
    qsort(nums1, nums1Size, sizeof(int), compare);
    qsort(nums2, nums2Size, sizeof(int), compare);
    
    int pos1 = 0, pos2 = 0; 
    int* result = malloc(sizeof(int)*nums1Size);
    *returnSize = 0;
    
    // 双指针。
    while (pos1 < nums1Size && pos2 < nums2Size) {
        if (nums1[pos1] < nums2[pos2])
            pos1++;
        else if (nums1[pos1] > nums2[pos2])
            pos2++;
        else {
            result[(*returnSize)++] = nums1[pos1];
            pos1++;
            pos2++;
        }
    }
    
    return result;
}

  | ps:此代码与 349. 两个数组的交集 — 力扣网 的差别只在于不用去重,即多加了记录 相同元素 个数的功能(可能出现多次,但是排序方法中体现不出差别)。


附录

  • 我的个人博客:messi1002.top
  • 如有错误或疑惑之处 请联系 [email protected]
  • 所有题目解答:fork me on github

你可能感兴趣的:(LeetCode-C,LeetCode-C)