当前位置:网站首页>【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;
}
}
边栏推荐
- 厂商推送平台-华为接入
- 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
- 【LeetCode-278】第一个错误的版本
- JNI入门
- Manufacturer Push Platform-Huawei Access
- 自己动手写RISC-V的C编译器-02语法描述方法和递归下降解析
- IIC and SPI
- Minutes of OpenMLDB Meetup No.2
- 手把手导入企业项目(快速完成本地项目配置)
- buuctf hacknote
猜你喜欢

The whole process of Tinker access --- configuration

Day 85

C语言-6月12日-字符替换问题,将一个‘ ’替换为2个‘#’

【LeetCode-74】搜索二维矩阵

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

Interpretation of the paper: GAN and detection network multi-task/SOD-MTGAN: Small Object Detection via Multi-Task Generative Adversarial Network

Manufacturer Push Platform-Huawei Access

Scene-driven feature calculation method OpenMLDB, efficient implementation of "calculate first use"

Thesis unscramble TransFG: A Transformer Architecture for Fine - grained Recognition

基于微信小程序云开发实现的电商项目,可以自行定制开发
随机推荐
USB URB
JVM tuning and finishing
nepctf Nyan Cat 彩虹猫
杀死进程-查看防火墙状态
函数使用记录
JS事件循环机制
C语言-7月31日-指针的总结以及typedef关键字
Building a data ecology for feature engineering - Embrace the open source ecology, OpenMLDB fully opens up the MLOps ecological tool chain
微信小程序启动页的实现
buuctf hacknote
虚拟机更改IP地址
星盟-pwn-babyfmt
Here is a memorial
Vscode remote connection server terminal zsh+Oh-my-zsh + Powerlevel10 + Autosuggestions + Autojump + Syntax-highlighting
【LeetCode-202】快乐数
IndexError: index 9 is out of bounds for axis 0 with size 9;数组下标溢出问题
Getting Started with JNI
The Summer of Open Source 2022 is coming | Welcome to sign up for the OpenMLDB community project~
C语言-6月8日-给定一个字符数组‘i am a student’ 统计字符a的个数并进行输出
C语言-内存操作函数