350.两个数组的交集 II:哈希表/双指针

【LetMeFly】350.两个数组的交集 II:哈希表/双指针

力扣题目链接:https://leetcode.cn/problems/intersection-of-two-arrays-ii/

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

 

示例 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 的元素存储在磁盘上,内存是有限的,并且你不能一次加载所有的元素到内存中,你该怎么办?

方法一:哈希表

类似于LeetCode 349. 两个数组的交集,这道题同样可以使用哈希表来解决。

这次建立一个可以计数的哈希表,遍历一遍第一个数组,将第一个数组中的数字(及其出现次数)存入哈希表中

然后遍历一遍第二个数组,如果这个数在哈希表中存在,并且次数大于0,那么就将这个数添加到答案数组中,并将这个数在哈希表中出现的次数减一。

  • 时间复杂度$O(n+m)$,其中$n$是第一个数组中元素的个数,$m$是第二个数组中元素的个数
  • 空间复杂度$O(n)$

哈希表时空复杂度的常数较大,因此执行结果为:时间/空间 分别超过了6.90%,15.67%的提交。

AC代码

C++(使用map)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*
* @Author: LetMeFly
* @Date: 2022-10-02 09:04:56
* @LastEditors: LetMeFly
* @LastEditTime: 2022-10-02 09:07:45
*/
class Solution {
public:
vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {
unordered_map<int, int> ma;
for (int& t : nums1) {
ma[t]++;
}
vector<int> ans;
for (int& t : nums2) {
if (ma[t] > 0) {
ans.push_back(t);
ma[t]--;
}
}
return ans;
}
};

C++(使用set)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/*
* @Author: LetMeFly
* @Date: 2025-01-30 08:10:44
* @LastEditors: LetMeFly.xyz
* @LastEditTime: 2025-01-30 08:17:11
*/
class Solution {
public:
vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {
unordered_multiset<int> se(nums1.begin(), nums1.end());
vector<int> ans;
for (int t : nums2) {
unordered_multiset<int>::iterator it = se.find(t);
if (it != se.end()) {
ans.push_back(t);
se.erase(it);
}
}
return ans;
}
};

Python

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
'''
Author: LetMeFly
Date: 2025-01-30 08:18:20
LastEditors: LetMeFly.xyz
LastEditTime: 2025-01-30 08:20:54
'''
from typing import List
from collections import Counter

class Solution:
def intersect(self, nums1: List[int], nums2: List[int]) -> List[int]:
cnt = Counter(nums1)
ans = []
for t in nums2:
if cnt[t]:
ans.append(t)
cnt[t] -= 1
return ans

Java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/*
* @Author: LetMeFly
* @Date: 2025-01-30 08:21:39
* @LastEditors: LetMeFly.xyz
* @LastEditTime: 2025-01-30 08:25:09
*/
import java.util.HashMap;
import java.util.Map;
import java.util.ArrayList;
import java.util.List;

class Solution {
public int[] intersect(int[] nums1, int[] nums2) {
Map<Integer, Integer> ma = new HashMap<>();
for (int t : nums1) {
ma.merge(t, 1, Integer::sum);
}
List<Integer> temp = new ArrayList<>();
for (int t : nums2) {
int c = ma.getOrDefault(t, 0);
if (c > 0) {
temp.add(t);
ma.put(t, c - 1);
}
}
return temp.stream().mapToInt(i -> i).toArray();
}
}

Go

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/*
* @Author: LetMeFly
* @Date: 2025-01-30 08:26:09
* @LastEditors: LetMeFly.xyz
* @LastEditTime: 2025-01-30 08:27:29
*/
package main

func intersect(nums1 []int, nums2 []int) (ans []int) {
ma := map[int]int{}
for _, t := range nums1 {
ma[t]++
}
for _, t := range nums2 {
if ma[t] > 0 {
ans = append(ans, t)
ma[t]--
}
}
return
}

方法二:双指针

首先对两个数组分别排序,然后使用两个“指针”,分别指向第一个数组和第二个数组的 第一个元素。

当两个指针都在有效范围(不越界)内时,比较两个指针所指元素。

  • 若两元素相等,则将此元素添加到答案数组中,并将两个指针分别后移一位
  • 若两元素不等,则将指向较小元素的指针后移一位

即可。

  • 时间复杂度$O(n\log n + m\log m)$,其中$n$是第一个数组中元素的个数,$m$是第二个数组中元素的个数
  • 空间复杂度$O(\log n + \log m)$

执行结果为:时间/空间 分别超过了86.34%,93.57%的提交。

AC代码

C++

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution {
public:
vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {
sort(nums1.begin(), nums1.end());
sort(nums2.begin(), nums2.end());
int loc1 = 0, loc2 = 0;
vector<int> ans;
while (loc1 < nums1.size() && loc2 < nums2.size()) {
if (nums1[loc1] == nums2[loc2]) {
ans.push_back(nums1[loc1]);
loc1++, loc2++;
}
else if (nums1[loc1] < nums2[loc2]) {
loc1++;
}
else {
loc2++;
}
}
return ans;
}
};

同步发文于CSDN,原创不易,转载请附上原文链接哦~
Tisfy:https://letmefly.blog.csdn.net/article/details/127140436


350.两个数组的交集 II:哈希表/双指针
https://blog.letmefly.xyz/2022/10/02/LeetCode 0350.两个数组的交集II/
作者
Tisfy
发布于
2022年10月2日
许可协议