当前位置:网站首页>【LeetCode-350】两个数组的交集II
【LeetCode-350】两个数组的交集II
2022-08-11 05:30:00 【Ring*】
6.7 两个数组的交集II【350】
6.7.1 题目描述
给你两个整数数组 nums1 和 nums2 ,请你以数组形式返回两数组的交集。返回结果中每个元素出现的次数,应与元素在两个数组中都出现的次数一致(如果出现次数不一致,则考虑取较小值)。可以不考虑输出结果的顺序。
6.7.2 方法一:哈希表
由于同一个数字在两个数组中都可能出现多次,因此需要用哈希表存储每个数字出现的次数。对于一个数字,其在交集中出现的次数等于该数字在两个数组中出现次数的最小值。
首先遍历第一个数组,并在哈希表中记录第一个数组中的每个数字以及对应出现的次数,然后遍历第二个数组,对于第二个数组中的每个数字,如果在哈希表中存在这个数字,则将该数字添加到答案,并减少哈希表中该数字出现的次数。
为了降低空间复杂度,首先遍历较短的数组并在哈希表中记录每个数字以及对应出现的次数,然后遍历较长的数组得到交集。
class Solution {
public int[] intersect(int[] nums1, int[] nums2) {
if (nums1.length > nums2.length) {
return intersect(nums2, nums1);
}
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for (int num : nums1) {
int count = map.getOrDefault(num, 0) + 1;
map.put(num, count); // 统计出nums1各字符出现的频数
}
int[] intersection = new int[nums1.length];
int index = 0;
for (int num : nums2) {
int count = map.getOrDefault(num, 0);
if (count > 0) {
// nums2中该字符在nums1中有
intersection[index++] = num; // 存入数组
count--; // 将map中该字符个数减1
if (count > 0) {
// 减1之后还有则覆盖map中该字符数据
map.put(num, count);
} else {
// 没有了将该字符从map中移除
map.remove(num);
}
}
}
return Arrays.copyOfRange(intersection, 0, index);
}
}
复杂度分析
- 时间复杂度:O(m+n),其中 m 和 n 分别是两个数组的长度。需要遍历两个数组并对哈希表进行操作,哈希表操作的时间复杂度是O(1),因此总时间复杂度与两个数组的长度和呈线性关系。
- 空间复杂度:O(min(m,n)),其中 m 和 n 分别是两个数组的长度。对较短的数组进行哈希表的操作,哈希表的大小不会超过较短的数组的长度。为返回值创建一个数组 intersection,其长度为较短的数组的长度。
6.7.3 方法二:排序 + 双指针
如果两个数组是有序的,则可以使用双指针的方法得到两个数组的交集。
首先对两个数组进行排序,然后使用两个指针遍历两个数组。
初始时,两个指针分别指向两个数组的头部。每次比较两个指针指向的两个数组中的数字,如果两个数字不相等,则将指向较小数字的指针右移一位,如果两个数字相等,将该数字添加到答案,并将两个指针都右移一位。当至少有一个指针超出数组范围时,遍历结束。
class Solution {
public int[] intersect(int[] nums1, int[] nums2) {
Arrays.sort(nums1);
Arrays.sort(nums2);
int length1 = nums1.length, length2 = nums2.length;
int[] intersection = new int[Math.min(length1, length2)];
int index1 = 0, index2 = 0, index = 0;
while (index1 < length1 && index2 < length2) {
if (nums1[index1] < nums2[index2]) {
index1++;
} else if (nums1[index1] > nums2[index2]) {
index2++;
} else {
intersection[index] = nums1[index1];
index1++;
index2++;
index++;
}
}
return Arrays.copyOfRange(intersection, 0, index);
}
}
复杂度分析
结语
6.7.4 my answer—排序 + 双指针
class Solution {
public int[] intersect(int[] nums1, int[] nums2) {
Arrays.sort(nums1);
Arrays.sort(nums2);
List<Integer> list = new ArrayList<>();
int l1 = nums1.length;
int l2 = nums2.length;
int p1 = 0;
int p2 = 0;
for (int i = 0; i < l1+l2; i++) {
if(p1==l1 || p2==l2)break;
if(nums1[p1]==nums2[p2]){
list.add(nums1[p1]);
p1++;
p2++;
}else if(nums1[p1]>nums2[p2]){
p2++;
}else {
p1++;
}
}
int ans[] = new int[list.size()];
for (int i = 0; i < ans.length; i++) {
ans[i] = list.get(i);
}
return ans;
}
}
边栏推荐
猜你喜欢
随机推荐
Open Source Machine Learning Database OpenMLDB Contributor Program Fully Launched
Thesis unscramble TransFG: A Transformer Architecture for Fine - grained Recognition
USB URB
C语言-7月22日- NULL和nullptr的深入了解以及VScode对nullptr语句报错问题的解决
js 学习进阶(事件高级 pink老师教学笔记)
PyQt5中调用.ui转换的.py文件代码解释
Event Preview | On April 23, a number of wonderful sharing sessions of OpenMLDB will come, which will live up to the good time of the weekend
JS case exercise (classic case of teacher pink)
heap2 (tcache attack,house of orange)
USB in NRZI to encode the data
微信小程序启动页的实现
【剑指offer系列】面试题日记(前期题)
[Meetup Preview] OpenMLDB+OneFlow: Link feature engineering to model training to accelerate machine learning model development
The Summer of Open Source 2022 is coming | Welcome to sign up for the OpenMLDB community project~
连接数据库时出现WARN: Establishing SSL connection without server‘s identity verification is not recommended.
Day 70
C语言实现三子棋(代码详解)
Real-time Feature Computing Platform Architecture Methodology and Practice Based on OpenMLDB
OpenMLDB v0.5.0 released | Performance, cost, flexibility reach new heights
Day 71