当前位置:网站首页>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;
}
};
边栏推荐
- How to customize animation avatars? These six free online cartoon avatar generators are exciting at a glance!
- C # use Marshall to manually create unmanaged memory in the heap and use
- The second day of rhcsa study
- R language uses the order function to sort the dataframe data, and descending sorting based on a single field (variable)
- 包装行业商业供应链管理平台解决方案:布局智慧供应体系,数字化整合包装行业供应链
- 今日直播 | “人玑协同 未来已来”2022弘玑生态伙伴大会蓄势待发
- CCNP Part 11 BGP (III) (essence)
- Meilu biological IPO was terminated: the annual revenue was 385million, and Chen Lin was the actual controller
- Simple understanding of MySQL database
- PMP practice once a day | don't get lost in the exam -7.6
猜你喜欢

五金机电行业供应商智慧管理平台解决方案:优化供应链管理,带动企业业绩增长

How to do smoke test

在解决了 2961 个用户反馈后,我做出了这样的改变...

零基础入门PolarDB-X:搭建高可用系统并联动数据大屏

LeetCode-1279. 红绿灯路口

Synchronous development of business and application: strategic suggestions for application modernization

Carte de réflexion + code source + notes + projet, saut d'octets + jd + 360 + tri des questions d'entrevue Netease

Solution of commercial supply chain management platform for packaging industry: layout smart supply system and digitally integrate the supply chain of packaging industry
Application of clock wheel in RPC

第五期个人能力认证考核通过名单公布
随机推荐
Sanmian ant financial successfully got the offer, and has experience in Android development agency recruitment and interview
快速幂模板求逆元,逆元的作用以及例题【第20届上海大学程序设计联赛夏季赛】排列计数
Test technology stack arrangement -- self cultivation of test development engineers
倒计时2天|腾讯云消息队列数据接入平台(Data Import Platform)直播预告
Swagger2 reports an error illegal DefaultValue null for parameter type integer
GCC【7】- 编译检查的是函数的声明,链接检查的是函数的定义bug
测试用里hi
Actf 2022 came to a successful conclusion, and 0ops team won the second consecutive championship!!
How to do smoke test
Use of map (the data of the list is assigned to the form, and the JSON comma separated display assignment)
LeetCode_ Double pointer_ Medium_ 61. rotating linked list
冒烟测试怎么做
LeetCode-1279. 红绿灯路口
short i =1; i=i+1与short i=1; i+=1的区别
Problems encountered in using RT thread component fish
[translation] Digital insider. Selection process of kubecon + cloudnativecon in Europe in 2022
黑马--Redis篇
包装行业商业供应链管理平台解决方案:布局智慧供应体系,数字化整合包装行业供应链
Pytorch common loss function
Druid database connection pool details