当前位置:网站首页>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;
}
};
边栏推荐
- MySQL information schema learning (I) -- general table
- 如何自定义动漫头像?这6个免费精品在线卡通头像生成器,看一眼就怦然心动!
- In depth analysis, Android interview real problem analysis is popular all over the network
- 全套教学资料,阿里快手拼多多等7家大厂Android面试真题
- zabbix 代理服务器 与 zabbix-snmp 监控
- short i =1; I=i+1 and short i=1; Difference of i+=1
- Excel 中VBA脚本的简单应用
- LeetCode_ Gray code_ Medium_ 89. Gray code
- Intelligent supply chain management system solution for hardware and electromechanical industry: digital intelligent supply chain "creates new blood" for traditional industries
- English topic assignment (25)
猜你喜欢
MySQL information schema learning (I) -- general table
如何自定义动漫头像?这6个免费精品在线卡通头像生成器,看一眼就怦然心动!
MRO industrial products enterprise procurement system: how to refine procurement collaborative management? Industrial products enterprises that want to upgrade must see!
三面蚂蚁金服成功拿到offer,Android开发社招面试经验
LeetCode-1279. Traffic light intersection
LeetCode_ Double pointer_ Medium_ 61. rotating linked list
Solution of intelligent management platform for suppliers in hardware and electromechanical industry: optimize supply chain management and drive enterprise performance growth
利用 clip-path 绘制不规则的图形
Synchronous development of business and application: strategic suggestions for application modernization
In depth analysis, Android interview real problem analysis is popular all over the network
随机推荐
How to type multiple spaces when editing CSDN articles
Looting iii[post sequence traversal and backtracking + dynamic planning]
打家劫舍III[后序遍历与回溯+动态规划]
保证接口数据安全的10种方案
手把手教你学会js的原型与原型链,猴子都能看懂的教程
Spark foundation -scala
Phoenix Architecture 3 - transaction processing
[pytorch] yolov5 train your own data set
Excel 中VBA脚本的简单应用
Computer network: sorting out common network interview questions (I)
在解决了 2961 个用户反馈后,我做出了这样的改变...
How to access localhost:8000 by mobile phone
蓝桥杯 微生物增殖 C语言
LeetCode-1279. 红绿灯路口
[玩转Linux] [Docker] MySQL安装和配置
Modulenotfounderror: no module named 'PIL' solution
Translation D28 (with AC code POJ 26:the nearest number)
Mind map + source code + Notes + project, ByteDance + JD +360+ Netease interview question sorting
通俗的讲解,带你入门协程
【翻译】云原生观察能力微调查。普罗米修斯引领潮流,但要了解系统的健康状况仍有障碍...