当前位置:网站首页>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;
}
};
边栏推荐
- 零基础入门PolarDB-X:搭建高可用系统并联动数据大屏
- R language ggplot2 visualization: use the ggdotplot function of ggpubr package to visualize dot plot, set the palette parameter, and set the colors of data points and box graphs of dot plots at differ
- [translation] linkerd's adoption rate in Europe and North America exceeded istio, with an increase of 118% in 2021.
- English topic assignment (25)
- The dplyr package of R language performs data grouping aggregation statistical transformations and calculates the grouping mean of dataframe data
- 学习探索-无缝轮播图
- Simple understanding of MySQL database
- 今日直播 | “人玑协同 未来已来”2022弘玑生态伙伴大会蓄势待发
- Simple application of VBA script in Excel
- LeetCode_ Double pointer_ Medium_ 61. rotating linked list
猜你喜欢
Reflection and illegalaccessexception exception during application
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]
零基础入门PolarDB-X:搭建高可用系统并联动数据大屏
IC设计流程中需要使用到的文件
Fast power template for inverse element, the role of inverse element and example [the 20th summer competition of Shanghai University Programming League] permutation counting
Dark horse -- redis
Interface test tool - postman
JDBC详解
冒烟测试怎么做
随机推荐
Interface test tool - postman
About image reading and processing, etc
Graffiti intelligence is listed on the dual main board in Hong Kong: market value of 11.2 billion Hong Kong, with an annual revenue of 300 million US dollars
JDBC详解
LeetCode-1279. 红绿灯路口
Mysql Information Schema 學習(一)--通用錶
10 schemes to ensure interface data security
Dark horse -- redis
Computer network: sorting out common network interview questions (I)
安装Mysql报错:Could not create or access the registry key needed for the...
ZABBIX proxy server and ZABBIX SNMP monitoring
1805. 字符串中不同整数的数目
Mysql Information Schema 学习(一)--通用表
map的使用(列表的数据赋值到表单,json逗号隔开显示赋值)
Mathematical knowledge -- code implementation of Gaussian elimination (elementary line transformation to solve equations)
LeetCode_ Double pointer_ Medium_ 61. rotating linked list
受益匪浅,安卓面试问题
tensorflow和torch代码验证cuda是否安装成功
How to customize animation avatars? These six free online cartoon avatar generators are exciting at a glance!
USB host driver - UVC swap