当前位置:网站首页>【 LeetCode 】 350. The intersection of two arrays. II
【 LeetCode 】 350. The intersection of two arrays. II
2022-07-29 15:03:00 【Crispy~】
题目
给你两个整数数组 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的元素存储在磁盘上,内存是有限的,并且你不能一次加载所有的元素到内存中,你该怎么办?
题解
使用哈希表(unordered_map)Stores the value and the number of occurrences of one of the arrays,Then find and compare one by one
//C++
class Solution {
public:
vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {
if(nums1.size() > nums2.size())//nums1存入哈希表中,Lookup is faster when the number of stores is small
return intersect(nums2,nums1);
unordered_map<int,int> mynums;//哈希表
//将nums1存入表中
for(int k:nums1)
{
mynums[k]++;
}
vector<int> result;//存储结果
//遍历nums2,Record when you meet the conditions
for(int k:nums2)
{
if(mynums.count(k) && mynums[k]!=0)
{
result.push_back(k);
mynums[k]--;
}
}
return result;
}
};
先排序,然后使用双指针法
class Solution {
public:
vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {
//排序
sort(nums1.begin(),nums1.end());
sort(nums2.begin(),nums2.end());
//存储结果
vector<int> result;
//初始化参数
int i=0;
int j=0;
int len1=nums1.size();
int len2=nums2.size();
//开始扫描
while(i<len1 && j<len2)
{
if(nums1[i]==nums2[j])//Recorded when the values pointed to by the two pointers are equal
{
result.push_back(nums1[i]);
i++;
j++;
}
else if(nums1[i]<nums2[j])//When not equal, the pointer with the smaller value is incremented by one,另一个不变
{
i++;
}
else
{
j++;
}
}
return result;
}
};
边栏推荐
- EA&UML日拱一卒-活动图::Variable Actions(续)
- Principles Of Mathematical Analysis, Third Edition免费下载地址
- 上个厕所的功夫,就把定时任务的三种调度策略说得明明白白
- Guangzhou fire: high temperature weather frequent fire fire safety should not be ignored
- 部门例会上做测试分享,不知道分享什么内容?
- The raised platform system based on JSP&Servlet implementation
- 这个 MySQL bug,99% 的人会踩坑!
- 中国互联网科技企业群狼围攻,谷歌终于遭受重挫导致利润大跌,它为推动鸿蒙的发展而后悔...
- 【C语言】AI三子棋的成长之路
- Violence recursion to dynamic programming 02 (very clever game of CARDS)
猜你喜欢

基于SSM实现在线聊天系统

潘多拉 IOT 开发板学习(RT-Thread)—— 实验19 MQTT 协议通信实验(学习笔记)

arcgis中编码方式改变引起的shp文件乱码、字符截断问题处理

第4章_1——SQL语句实现MySQL增删改查

C语言 5:bool类型,关系表达式,逻辑表达式,分支语句,函数调用机制,break,continue,goto,return/exit跳转语句

【yolov7系列二】正负样本分配策略

【Postman】Download and installation (novice graphic tutorial)

打卡广汽本田喜悦安全驾驶中心,体验最刁钻的场地训练

如何编辑CAD图库里的图纸

数字孪生万物可视 |联接现实世界与数字空间
随机推荐
测试日报怎么写 ?
中国互联网科技企业群狼围攻,谷歌终于遭受重挫导致利润大跌,它为推动鸿蒙的发展而后悔...
这 6 款在线 PDF 转换工具,得试
mysql datetime格式化日期(mysql start with)
微服务实战|集中配置中心Config非对称加密与安全管理
【Postman】下载与安装(新手图文教程)
rosbag数据画图MATLAB
Linux installation of MySQL (super detailed)
EA&UML日拱一卒-活动图::Feature和StuctualFeature
Interfaces and Abstractions
AQS源码阅读与强软弱虚4种引用以及ThreadLocal原理与源码
Google Play 政策更新 | 2022 年 7 月
《外太空的莫扎特》
用Asm生成Class字节码文件
求教一下 现在最新版的flinkcdc能获取到oracle的ddl变更信息吗?
C语言 5:bool类型,关系表达式,逻辑表达式,分支语句,函数调用机制,break,continue,goto,return/exit跳转语句
潘多拉 IOT 开发板学习(RT-Thread)—— 实验19 MQTT 协议通信实验(学习笔记)
代码越写越乱?那是因为你没用责任链
Violence recursion to dynamic programming 02 (very clever game of CARDS)
三 RedisTemplate 序列化机制配置实战