当前位置:网站首页>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;
}
};
边栏推荐
- Vmware虚拟机无法打开内核设备“\\.\Global\vmx86“的解决方法
- 【翻译】Linkerd在欧洲和北美的采用率超过了Istio,2021年增长118%。
- Unbalance balance (dynamic programming, DP)
- 快速幂模板求逆元,逆元的作用以及例题【第20届上海大学程序设计联赛夏季赛】排列计数
- 终于可以一行代码也不用改了!ShardingSphere 原生驱动问世
- Hudi vs Delta vs Iceberg
- GCC【7】- 编译检查的是函数的声明,链接检查的是函数的定义bug
- Use of deg2rad and rad2deg functions in MATLAB
- 【翻译】供应链安全项目in-toto移至CNCF孵化器
- 在解决了 2961 个用户反馈后,我做出了这样的改变...
猜你喜欢
In depth analysis, Android interview real problem analysis is popular all over the network

Interview assault 63: how to remove duplication in MySQL?

10 schemes to ensure interface data security

How to customize animation avatars? These six free online cartoon avatar generators are exciting at a glance!

JDBC details
受益匪浅,安卓面试问题
Interview assault 63: how to remove duplication in MySQL?

Cereals Mall - Distributed Advanced p129~p339 (end)

How to type multiple spaces when editing CSDN articles

IC设计流程中需要使用到的文件
随机推荐
MATLAB中deg2rad和rad2deg函数的使用
10 schemes to ensure interface data security
Mind map + source code + Notes + project, ByteDance + JD +360+ Netease interview question sorting
Computer network: sorting out common network interview questions (I)
保证接口数据安全的10种方案
Phoenix Architecture 2 - accessing remote services
理解 YOLOV1 第二篇 预测阶段 非极大值抑制(NMS)
时钟轮在 RPC 中的应用
学习探索-函数防抖
Black Horse - - Redis Chapter
spark基础-scala
Lick the dog until the last one has nothing (simple DP)
Interview assault 63: how to remove duplication in MySQL?
【翻译】云原生观察能力微调查。普罗米修斯引领潮流,但要了解系统的健康状况仍有障碍...
Chic Lang: attributeerror: partially initialized module 'CV2' has no attribute 'GAPI_ wip_ gst_ GStreamerPipe
谷粒商城--分布式高级篇P129~P339(完结)
121. 买卖股票的最佳时机
C # - realize serialization with Marshall class
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
Mysql Information Schema 学习(二)--Innodb表