题目:
Given two arrays, write a function to compute their intersection.

Example:
Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2, 2].

Note:
Each element in the result should appear as many times as it shows in both arrays.
The result can be in any order.
Follow up:
What if the given array is already sorted? How would you optimize your algorithm?
What if nums1’s size is small compared to nums2’s size? Which algorithm is better?
What if elements of nums2 are stored on disk, and the memory is limited such that you cannot load all elements into the memory at once?

思路:
这题和LeetCode 349. Intersection of Two Arrays思路一模一样,只需要将那题中的set换成multiset即可。具体思路见注释。

代码:

class Solution {
public:vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {vector<int> result;multiset<int> setnums1(nums1.begin(),nums1.end());//将两个vector的数值放到set里去年重合的multiset<int> setnums2(nums2.begin(),nums2.end());for(set<int>::iterator iter1=setnums1.begin(),iter2=setnums2.begin();iter1!=setnums1.end()&&iter2!=setnums2.end();){//分别从两个multiset从头往后循环,如果都没有到结尾(有一个到结尾就跳出循环)if(*iter1==*iter2){//如果两个值相等result.push_back(*iter1);//这个值就为重合的++iter1;//iter1和iter2都后移++iter2;}else if(*iter1<*iter2){//如果*iter1值小,iter1后移++iter1;}else if(*iter1>*iter2){//如果*iter2值小,iter2后移++iter2;}}return result;}
};

输出结果: 6ms

LeetCode 350. Intersection of Two Arrays II相关推荐

  1. 350.Intersection of Two Arrays II

    Given two arrays, write a function to compute their intersection. 分析: 本题与349题较为相似,唯一不同之处在于交集允许出现重复数字 ...

  2. 350. Intersection of Two Arrays II 两个数组的交集 II

    Title 给定两个数组,编写一个函数来计算它们的交集. 示例 1: 输入: nums1 = [1,2,2,1], nums2 = [2,2] 输出: [2,2] 示例 2: 输入: nums1 = ...

  3. LeetCode算法题350:两个数组的交集II(Intersection of Two Arrays II)

    技术交流可以加: 本人微信:xcg852390212 本人qq:852390212 学习交流qq群1(已满): 962535112 学习交流qq群2: 780902027 两个数组的交集II Leet ...

  4. C#LeetCode刷题之#350-两个数组的交集 II(Intersection of Two Arrays II)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4044 访问. 给定两个数组,编写一个函数来计算它们的交集. 输入 ...

  5. leetcode 349. Intersection of Two Arrays 1

    Description Given two arrays, write a function to compute their intersection. Example: Given nums1 = ...

  6. leetcode 349. Intersection of Two Arrays

    Given two arrays, write a function to compute their intersection. Example: Given nums1 = [1, 2, 2, 1 ...

  7. LeetCode之Intersection of Two Arrays

    1.题目 Given two arrays, write a function to compute their intersection. Example: Given nums1 = [1, 2, ...

  8. [LeetCode] Intersection of Two Arrays 两个数组相交

    Given two arrays, write a function to compute their intersection. Example: Given nums1 = [1, 2, 2, 1 ...

  9. 【LeetCode】剑指 Offer 59 - II. 队列的最大值

    [LeetCode]剑指 Offer 59 - II. 队列的最大值 文章目录 [LeetCode]剑指 Offer 59 - II. 队列的最大值 package offer;import java ...

最新文章

  1. QA发现的令人抓狂的BUG
  2. 如何识别架构方案是否合理
  3. python访问k8s的api_如何通过Python Kubernetes客户端连接microk8s API?
  4. OpenCV向应用程序添加跟踪栏
  5. 只包含因子2 3 5的数(51nod 思维 打表)
  6. c语言递归函数检测回文,在C中使用递归函数的回文测试
  7. python数据分析常用包之Scipy
  8. 如何创建基本的高级队列之一:创建发送方代码
  9. 48_并发编程-线程-资源共享/锁
  10. UIScrollView加约束
  11. 在git上面找开源项目遇到的坑
  12. Redis——学习之路三(初识redis config配置)
  13. c# foreach循环二维数组
  14. linux救援模式详解,Linux系统的救援模式应用详解
  15. 【技术分享】数据之大,云动未来 - 弹性云数据库技术解析
  16. 07-HTML通用属性
  17. 数据结构与算法入门——二叉树之平衡二叉树单螺旋双螺旋
  18. 【181125】VC++ 绘制立方体的生成与控制旋转程序源代码
  19. ESP8266(ESP-12F) 第三方库使用 -- SparkFun_APDS9960 (手势识别)
  20. RS|高分1号影像预处理

热门文章

  1. Win10+Torch1.9+CUDA11.1成功配置YOLOX预测环境
  2. js点击复制文本 - 案例篇
  3. phpcmsV9推荐位posid文章,图片不显示问题 - 思路篇
  4. dedeCMS初始化数据体验包的安装与清除?
  5. 单机android游戏排行榜,安卓单机手机游戏推荐_十大必玩单机手机游戏
  6. oracle类似sqlcmd工具,Oracle:是否有跟踪查询的工具,例如用于SQL Server的Profiler?...
  7. http代码_一行代码就可以实现HTTP文件服务器,他为什么写了150行?
  8. 大数据分布式工作流任务调度系统DolphinScheduler v1.3.4
  9. 雷鸟下载器v3.0.0 百度网盘第三方不限速下载工具
  10. 怎样直接在列表修改数据html,谈谈yii2-GridView如何实现列表页直接修改数据