博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LC-349 两个数组的交集
阅读量:4634 次
发布时间:2019-06-09

本文共 693 字,大约阅读时间需要 2 分钟。

目标:

输入两个数组,输出它们的交集。

 

思路:

用一个set来保存结果,遍历数组一中的元素是否数组二中存在。

 

代码:

1 class Solution { 2 public: 3     vector
intersection(vector
& nums1, vector
& nums2) { 4 set
result; 5 for (int i = 0; i < nums1.size(); i++) { 6 for (int j = 0; j < nums2.size(); j++) { 7 if (nums1[i] == nums2[j]) { 8 result.insert(nums1[i]); 9 break;10 }11 }12 }13 vector
v(result.begin(), result.end());14 return v;15 }16 };

 

转载于:https://www.cnblogs.com/leo-lzj/p/10243675.html

你可能感兴趣的文章
react组件回顶部
查看>>
MyBatis if标签的用法
查看>>
VMware
查看>>
具体解释可变參数列表
查看>>
【LeetCode】Palindrome Partitioning 解题报告
查看>>
用vue-cli脚手架搭建一个仿网易云音乐的全家桶vue项目
查看>>
Solution for Concurrent number of AOS' for this application exceeds the licensed number
查看>>
从壹开始微服务 [ DDD ] 之一 ║ D3模式设计初探 与 我的计划书
查看>>
python 错误之SyntaxError: Missing parentheses in call to 'print'
查看>>
Windows Phone开发(16):样式和控件模板
查看>>
CSE 3100 Systems Programming
查看>>
洛谷 1604——B进制星球(高精度算法)
查看>>
IntelliJ IDEA 的Project structure说明
查看>>
处理 JSON null 和空数组及对象
查看>>
Java Security(JCE基本概念)
查看>>
服务注册发现consul之四: 分布式锁之四:基于Consul的KV存储和分布式信号量实现分布式锁...
查看>>
使用Java监控工具出现 Can't attach to the process
查看>>
递归与非递归转换(栈知识应用)
查看>>
asp.net mvc 学习
查看>>
Java动态代理和静态代理区别
查看>>