当前位置:网站首页>【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;
}
}
边栏推荐
- Simple mine sweeping in C language (with source code)
- Minutes of OpenMLDB Meetup No.2
- swagger常用注释API @ApiModel、@ApiModelProperty的用法
- The Summer of Open Source 2022 is coming | Welcome to sign up for the OpenMLDB community project~
- Matplotlib找不到字体,打印乱码
- Day 71
- Wonderful linkage | OpenMLDB Pulsar Connector principle and practical operation
- Manufacturer Push Platform-Huawei Access
- Day 76
- Regular expression replacement for batch quick modification code
猜你喜欢

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

C语言-7月19日-指针的学习

Day 83

Day 87

将一个excel文件中多个sheet页“拆分“成多个“独立“excel文件

父子节点数据格式不一致的树状列表实现

three.js基础学习

无效的修订:3.18.1-g262b901-dirty

buuctf hacknote

Wonderful linkage | OpenMLDB Pulsar Connector principle and practical operation
随机推荐
C language implementation guess Numbers (with source code, can be directly run)
解决npm warn config global `--global`, `--local` are deprecated. use `--location=global` instead.
Goldbach's conjecture and the ring of integers
Open Source Machine Learning Database OpenMLDB Contributor Program Fully Launched
Jetpack's dataBinding
Wonderful linkage | OpenMLDB Pulsar Connector principle and practical operation
JVM学习四:垃圾收集器与内存回收策略
【LeetCode-202】快乐数
ARM assembly instruction ADR and LDR
USB URB
详解程序执行过程
OpenMLDB Pulsar Connector: Efficiently connect real-time data to feature engineering
Simple mine sweeping in C language (with source code)
OpenMLDB: Consistent production-level feature computing platform online and offline
The whole process of Tinker access --- Compilation
品优购项目实战笔记
微信小程序启动页的实现
C语言实现三子棋(代码详解)
C语言-内存操作函数
【LeetCode-69】x的平方根