当前位置:网站首页>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;
}
};
边栏推荐
- [translation] micro survey of cloud native observation ability. Prometheus leads the trend, but there are still obstacles to understanding the health of the system
- R language uses the order function to sort the dataframe data, and descending sorting based on a single field (variable)
- Mysql Information Schema 學習(一)--通用錶
- C # use Marshall to manually create unmanaged memory in the heap and use
- 学习探索-函数防抖
- 深入分析,Android面试真题解析火爆全网
- 【翻译】供应链安全项目in-toto移至CNCF孵化器
- 打家劫舍III[后序遍历与回溯+动态规划]
- Analysis of frequent chain breaks in applications using Druid connection pools
- Simple application of VBA script in Excel
猜你喜欢
Sanmian ant financial successfully got the offer, and has experience in Android development agency recruitment and interview
Simple understanding of MySQL database
In depth analysis, Android interview real problem analysis is popular all over the network
接雨水问题解析
Using clip path to draw irregular graphics
保证接口数据安全的10种方案
php+redis实现超时取消订单功能
Synchronous development of business and application: strategic suggestions for application modernization
CCNP Part 11 BGP (III) (essence)
Low CPU load and high loadavg processing method
随机推荐
倒计时2天|腾讯云消息队列数据接入平台(Data Import Platform)直播预告
How to type multiple spaces when editing CSDN articles
在解决了 2961 个用户反馈后,我做出了这样的改变...
Intelligent supply chain management system solution for hardware and electromechanical industry: digital intelligent supply chain "creates new blood" for traditional industries
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
凤凰架构3——事务处理
Lick the dog until the last one has nothing (simple DP)
zabbix 代理服务器 与 zabbix-snmp 监控
The list of people who passed the fifth phase of personal ability certification assessment was published
C # - realize serialization with Marshall class
Interface test tool - postman
CPU负载很低,loadavg很高处理方法
Simple understanding of MySQL database
利用 clip-path 绘制不规则的图形
第五期个人能力认证考核通过名单公布
It's super detailed in history. It's too late for you to read this information if you want to find a job
通俗的讲解,带你入门协程
[translation] linkerd's adoption rate in Europe and North America exceeded istio, with an increase of 118% in 2021.
LeetCode_ Double pointer_ Medium_ 61. rotating linked list
快速幂模板求逆元,逆元的作用以及例题【第20届上海大学程序设计联赛夏季赛】排列计数