当前位置:网站首页>350. Intersection of two arrays II
350. Intersection of two arrays II
2022-07-06 19:36:00 【yitahutu79】
Here are two arrays of integers nums1 and nums2 , Please return the intersection of two arrays as an array . Returns the number of occurrences of each element in the result , It should be consistent with the number of occurrences of elements in both arrays ( If the number of occurrences is inconsistent , Then consider taking the smaller value ). You can ignore the order of the output results .
Example 1:
Input :nums1 = [1,2,2,1], nums2 = [2,2]
Output :[2,2]
Example 2:
Input :nums1 = [4,9,5], nums2 = [9,4,9,8,4]
Output :[4,9]
Tips :
1 <= nums1.length, nums2.length <= 1000
0 <= nums1[i], nums2[i] <= 1000
Advanced :
What if the given array has been ordered ? How will you optimize your algorithm ?
If nums1 Size ratio nums2 Small , Which method is better ?
If nums2 Elements of are stored on disk , Memory is limited , And you can't load all the elements into memory at once , What are you gonna do? ?
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;
}
};
边栏推荐
- 理解 YOLOV1 第二篇 预测阶段 非极大值抑制(NMS)
- English topic assignment (25)
- Cereals Mall - Distributed Advanced p129~p339 (end)
- Interview assault 63: how to remove duplication in MySQL?
- Solution of intelligent management platform for suppliers in hardware and electromechanical industry: optimize supply chain management and drive enterprise performance growth
- LeetCode_双指针_中等_61. 旋转链表
- Benefit a lot, Android interview questions
- 快速幂模板求逆元,逆元的作用以及例题【第20届上海大学程序设计联赛夏季赛】排列计数
- Interview assault 63: how to remove duplication in MySQL?
- [translation] micro survey of cloud native observation ability. Prometheus leads the trend, but there are still obstacles to understanding the health of the system
猜你喜欢
RT-Thread 组件 FinSH 使用时遇到的问题
通俗的讲解,带你入门协程
理解 YOLOV1 第二篇 预测阶段 非极大值抑制(NMS)
Take a look at how cabloyjs workflow engine implements activiti boundary events
Sanmian ant financial successfully got the offer, and has experience in Android development agency recruitment and interview
[玩转Linux] [Docker] MySQL安装和配置
系统性详解Redis操作Hash类型数据(带源码分析及测试结果)
面试突击63:MySQL 中如何去重?
10 schemes to ensure interface data security
安装Mysql报错:Could not create or access the registry key needed for the...
随机推荐
CCNP Part 11 BGP (III) (essence)
Fast power template for inverse element, the role of inverse element and example [the 20th summer competition of Shanghai University Programming League] permutation counting
Tensorflow and torch code verify whether CUDA is successfully installed
C # use Marshall to manually create unmanaged memory in the heap and use
关于图像的读取及处理等
Carte de réflexion + code source + notes + projet, saut d'octets + jd + 360 + tri des questions d'entrevue Netease
倒计时2天|腾讯云消息队列数据接入平台(Data Import Platform)直播预告
Mysql Information Schema 学习(二)--Innodb表
Tensorflow2.0 自定义训练的方式求解函数系数
swagger2报错Illegal DefaultValue null for parameter type integer
Take a look at how cabloyjs workflow engine implements activiti boundary events
MATLAB中deg2rad和rad2deg函数的使用
RT-Thread 组件 FinSH 使用时遇到的问题
思維導圖+源代碼+筆記+項目,字節跳動+京東+360+網易面試題整理
Mind map + source code + Notes + project, ByteDance + JD +360+ Netease interview question sorting
Is not a drawable (color or path): the vector graph downloaded externally cannot be called when it is put into mipmap, and the calling error program crashes
GCC【7】- 编译检查的是函数的声明,链接检查的是函数的定义bug
Chic Lang: attributeerror: partially initialized module 'CV2' has no attribute 'GAPI_ wip_ gst_ GStreamerPipe
Dark horse -- redis
[translation] supply chain security project in toto moved to CNCF incubator