当前位置:网站首页>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;
}
};
边栏推荐
- Live broadcast today | the 2022 Hongji ecological partnership conference of "Renji collaboration has come" is ready to go
- JDBC详解
- Intelligent supply chain management system solution for hardware and electromechanical industry: digital intelligent supply chain "creates new blood" for traditional industries
- Reflection and illegalaccessexception exception during application
- Excel 中VBA脚本的简单应用
- [translation] a GPU approach to particle physics
- GCC [7] - compilation checks the declaration of functions, and link checks the definition bugs of functions
- Zero foundation entry polardb-x: build a highly available system and link the big data screen
- Looting iii[post sequence traversal and backtracking + dynamic planning]
- Mysql Information Schema 学习(二)--Innodb表
猜你喜欢

zabbix 代理服务器 与 zabbix-snmp 监控
Interview assault 63: how to remove duplication in MySQL?

spark基础-scala

利用 clip-path 绘制不规则的图形
Application of clock wheel in RPC

冒烟测试怎么做

Solution of intelligent management platform for suppliers in hardware and electromechanical industry: optimize supply chain management and drive enterprise performance growth

Spark foundation -scala

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

Cereals Mall - Distributed Advanced p129~p339 (end)
随机推荐
Solution of intelligent management platform for suppliers in hardware and electromechanical industry: optimize supply chain management and drive enterprise performance growth
Mysql Information Schema 學習(一)--通用錶
快速幂模板求逆元,逆元的作用以及例题【第20届上海大学程序设计联赛夏季赛】排列计数
Solution of commercial supply chain management platform for packaging industry: layout smart supply system and digitally integrate the supply chain of packaging industry
C # use Marshall to manually create unmanaged memory in the heap and use
倒计时2天|腾讯云消息队列数据接入平台(Data Import Platform)直播预告
黑馬--Redis篇
终于可以一行代码也不用改了!ShardingSphere 原生驱动问世
PMP practice once a day | don't get lost in the exam -7.6
Modulenotfounderror: no module named 'PIL' solution
MySQL information Schema Learning (i) - - General table
Actf 2022 came to a successful conclusion, and 0ops team won the second consecutive championship!!
In depth analysis, Android interview real problem analysis is popular all over the network
Chic Lang: attributeerror: partially initialized module 'CV2' has no attribute 'GAPI_ wip_ gst_ GStreamerPipe
关于图像的读取及处理等
Lick the dog until the last one has nothing (simple DP)
[translation] Digital insider. Selection process of kubecon + cloudnativecon in Europe in 2022
map的使用(列表的数据赋值到表单,json逗号隔开显示赋值)
Interpretation of Dagan paper
The slave i/o thread stops because master and slave have equal MySQL serv