当前位置:网站首页>350. 两个数组的交集 II
350. 两个数组的交集 II
2022-07-06 11:36:00 【yitahutu79】
给你两个整数数组 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 的元素存储在磁盘上,内存是有限的,并且你不能一次加载所有的元素到内存中,你该怎么办?
class Solution {
public:
vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {
sort(nums1.begin(),nums1.end());
sort(nums2.begin(),nums2.end());
int m = nums1.size();
int n = nums2.size();
int n1 = 0, n2 =0;
vector<int> arr;
while(n1 < m && n2 < n){
if (nums1[n1] == nums2[n2]) {
arr.push_back(nums1[n1]);
n1++;
n2++;
} else if (nums1[n1] < nums2[n2]) {
n1++;
} else n2++;
}
return arr;
}
};
边栏推荐
- PMP practice once a day | don't get lost in the exam -7.6
- Don't miss this underestimated movie because of controversy!
- 【计算情与思】扫地僧、打字员、信息恐慌与奥本海默
- Interface test tool - postman
- Simple application of VBA script in Excel
- Pytorch common loss function
- Solution of commercial supply chain management platform for packaging industry: layout smart supply system and digitally integrate the supply chain of packaging industry
- 黑马--Redis篇
- In depth analysis, Android interview real problem analysis is popular all over the network
- 全套教学资料,阿里快手拼多多等7家大厂Android面试真题
猜你喜欢

JDBC详解

【基础架构】Flink/Flink-CDC的部署和配置(MySQL / ES)

五金机电行业智能供应链管理系统解决方案:数智化供应链为传统产业“造新血”

学习探索-无缝轮播图

快速幂模板求逆元,逆元的作用以及例题【第20届上海大学程序设计联赛夏季赛】排列计数
In depth analysis, Android interview real problem analysis is popular all over the network

凤凰架构3——事务处理

谷粒商城--分布式高级篇P129~P339(完结)

【翻译】云原生观察能力微调查。普罗米修斯引领潮流,但要了解系统的健康状况仍有障碍...

MRO industrial products enterprise procurement system: how to refine procurement collaborative management? Industrial products enterprises that want to upgrade must see!
随机推荐
Druid database connection pool details
1805. 字符串中不同整数的数目
学习探索-函数防抖
ACTF 2022圆满落幕,0ops战队二连冠!!
A popular explanation will help you get started
The dplyr package of R language performs data grouping aggregation statistical transformations and calculates the grouping mean of dataframe data
Sanmian ant financial successfully got the offer, and has experience in Android development agency recruitment and interview
DaGAN论文解读
IC设计流程中需要使用到的文件
Pychrm Community Edition calls matplotlib pyplot. Solution of imshow() function image not popping up
usb host 驱动 - UVC 掉包
Use of map (the data of the list is assigned to the form, and the JSON comma separated display assignment)
史上超级详细,想找工作的你还不看这份资料就晚了
Zero foundation entry polardb-x: build a highly available system and link the big data screen
Yyds dry goods inventory leetcode question set 751 - 760
[玩转Linux] [Docker] MySQL安装和配置
R language uses rchisq function to generate random numbers that conform to Chi square distribution, and uses plot function to visualize random numbers that conform to Chi square distribution
Mysql Information Schema 学习(二)--Innodb表
First day of rhcsa study
Looting iii[post sequence traversal and backtracking + dynamic planning]