当前位置:网站首页>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;
}
};
边栏推荐
- Countdown 2 days | live broadcast preview of Tencent cloud message queue data import platform
- English topic assignment (25)
- php+redis实现超时取消订单功能
- 保证接口数据安全的10种方案
- 冒烟测试怎么做
- [pytorch] yolov5 train your own data set
- CPU负载很低,loadavg很高处理方法
- Translation D28 (with AC code POJ 26:the nearest number)
- Test technology stack arrangement -- self cultivation of test development engineers
- 测试用里hi
猜你喜欢
Benefit a lot, Android interview questions
MySQL information schema learning (II) -- InnoDB table
spark基础-scala
Detailed idea and code implementation of infix expression to suffix expression
保证接口数据安全的10种方案
Learning and Exploration - Seamless rotation map
[translation] micro survey of cloud native observation ability. Prometheus leads the trend, but there are still obstacles to understanding the health of the system
Interpretation of Dagan paper
[玩转Linux] [Docker] MySQL安装和配置
A method of removing text blur based on pixel repair
随机推荐
Phoenix Architecture 2 - accessing remote services
[pytorch] yolov5 train your own data set
【翻译】云原生观察能力微调查。普罗米修斯引领潮流,但要了解系统的健康状况仍有障碍...
How to type multiple spaces when editing CSDN articles
1805. 字符串中不同整数的数目
Leetcode 30. 串联所有单词的子串
Druid database connection pool details
手把手教你学会js的原型与原型链,猴子都能看懂的教程
Tensorflow2.0 自定义训练的方式求解函数系数
通俗的讲解,带你入门协程
[玩转Linux] [Docker] MySQL安装和配置
usb host 驱动 - UVC 掉包
The second day of rhcsa study
Yyds dry goods inventory leetcode question set 751 - 760
php+redis实现超时取消订单功能
How to customize animation avatars? These six free online cartoon avatar generators are exciting at a glance!
Intelligent supply chain management system solution for hardware and electromechanical industry: digital intelligent supply chain "creates new blood" for traditional industries
思維導圖+源代碼+筆記+項目,字節跳動+京東+360+網易面試題整理
LeetCode_ Double pointer_ Medium_ 61. rotating linked list
Translation D28 (with AC code POJ 26:the nearest number)