两个数组的交集 II

描述

  • 给你两个整数数组 nums1 和 nums2 ,请你以数组形式返回两数组的交集。
  • 返回结果中每个元素出现的次数,应与元素在两个数组中都出现的次数一致(如果出现次数不一致,则考虑取较小值)。
  • 可以不考虑输出结果的顺序。

示例 1

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

示例 2

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

提示

  • 1 <= nums1.length, nums2.length <= 1000
  • 0 <= nums1[i], nums2[i] <= 1000

进阶

  • 如果给定的数组已经排好序呢?你将如何优化你的算法?
  • 如果 nums1 的大小比 nums2 小,哪种方法更优?
  • 如果 nums2 的元素存储在磁盘上,内存是有限的,并且你不能一次加载所有的元素到内存中,你该怎么办?

Typescript 算法实现


1 ) 方案1:哈希表

function intersect(nums1: number[], nums2: number[]): number[] {
    if (nums1.length > nums2.length) {
        return intersect(nums2, nums1);
    }
    
    const map = new Map<number, number>();
    for (const num of nums1) {
        map.set(num, (map.get(num) || 0) + 1);
    }
    
    const intersection: number[] = [];
    for (const num of nums2) {
        if (map.has(num) && map.get(num)! > 0) {
            intersection.push(num);
            map.set(num, map.get(num)! - 1);
        }
    }
    
    return intersection;
}

2 )方案2: 排序 + 双指针

function intersect(nums1: number[], nums2: number[]): number[] {
    nums1.sort((a, b) => a - b);
    nums2.sort((a, b) => a - b);
    const intersection: number[] = [];
    let i = 0, j = 0;
    while (i < nums1.length && j < nums2.length) {
        if (nums1[i] < nums2[j]) {
            i++;
        } else if (nums1[i] > nums2[j]) {
            j++;
        } else {
            intersection.push(nums1[i]);
            i++;
            j++;
        }
    }
    return intersection;
}

更多推荐