当前位置:网站首页>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;
}
};
边栏推荐
- 黑馬--Redis篇
- CF960G - Bandit Blues(第一类斯特林数+OGF)
- The dplyr package of R language performs data grouping aggregation statistical transformations and calculates the grouping mean of dataframe data
- usb host 驱动 - UVC 掉包
- About image reading and processing, etc
- 10 schemes to ensure interface data security
- Druid 数据库连接池 详解
- R language uses DT function to generate t-distribution density function data and plot function to visualize t-distribution density function data
- 全套教学资料,阿里快手拼多多等7家大厂Android面试真题
- Xingnuochi technology's IPO was terminated: it was planned to raise 350million yuan, with an annual revenue of 367million yuan
猜你喜欢
It's super detailed in history. It's too late for you to read this information if you want to find a job
Computer network: sorting out common network interview questions (I)
Meilu biological IPO was terminated: the annual revenue was 385million, and Chen Lin was the actual controller
学习探索-使用伪元素清除浮动元素造成的高度坍塌
倒计时2天|腾讯云消息队列数据接入平台(Data Import Platform)直播预告
C language daily practice - day 22: Zero foundation learning dynamic planning
Druid database connection pool details
JDBC details
反射及在运用过程中出现的IllegalAccessException异常
面试突击63:MySQL 中如何去重?
随机推荐
Swagger2 reports an error illegal DefaultValue null for parameter type integer
助力安全人才专业素养提升 | 个人能力认证考核第一阶段圆满结束!
1805. 字符串中不同整数的数目
map的使用(列表的数据赋值到表单,json逗号隔开显示赋值)
The slave i/o thread stops because master and slave have equal MySQL serv
反射及在运用过程中出现的IllegalAccessException异常
Live broadcast today | the 2022 Hongji ecological partnership conference of "Renji collaboration has come" is ready to go
LeetCode_ Gray code_ Medium_ 89. Gray code
如何自定义动漫头像?这6个免费精品在线卡通头像生成器,看一眼就怦然心动!
MATLAB中deg2rad和rad2deg函数的使用
思維導圖+源代碼+筆記+項目,字節跳動+京東+360+網易面試題整理
ModuleNotFoundError: No module named ‘PIL‘解决方法
Intelligent supply chain management system solution for hardware and electromechanical industry: digital intelligent supply chain "creates new blood" for traditional industries
C # use Marshall to manually create unmanaged memory in the heap and use
Take a look at how cabloyjs workflow engine implements activiti boundary events
CCNP Part 11 BGP (III) (essence)
Unbalance balance (dynamic programming, DP)
Black Horse - - Redis Chapter
Php+redis realizes the function of canceling orders over time
LeetCode_双指针_中等_61. 旋转链表