349. Intersection of Two Arrays
来源:互联网 发布:使用隐藏的域名转发 编辑:程序博客网 时间:2024/06/18 18:53
题目:
Given two arrays, write a function to compute their intersection.
Example:
Given nums1 = [1, 2, 2, 1]
, nums2 = [2, 2]
, return [2]
.
Note:
- Each element in the result must be unique.
- The result can be in any order.
思路:
1.去除重复值
2.查找交叉项
利用C++ set标准数据结构,并巧用find查找函数
代码:
class Solution {public: vector<int> intersection(vector<int>& nums1, vector<int>& nums2) { vector<int> res; set<int> num1; set<int> num2; for(int i = 0;i<nums1.size();i++) num1.insert(nums1[i]); for(int i = 0;i<nums2.size();i++) num2.insert(nums2[i]); for(set<int>::iterator itr = num2.begin();itr!=num2.end();itr++) { if(num1.find(*itr)!=num1.end()) res.push_back(*itr); } return res; }};
阅读全文
1 0
- 349. Intersection of Two Arrays
- 349. Intersection of Two Arrays[][
- 349. Intersection of Two Arrays
- 349. Intersection of Two Arrays
- 349. Intersection of Two Arrays
- 349. Intersection of Two Arrays
- 349. Intersection of Two Arrays
- 349. Intersection of Two Arrays
- 349. Intersection of Two Arrays
- 349. Intersection of Two Arrays
- 349. Intersection of Two Arrays
- 349. Intersection of Two Arrays
- 349. Intersection of Two Arrays
- 349. Intersection of Two Arrays
- 349. Intersection of Two Arrays
- 349. Intersection of Two Arrays
- 349. Intersection of Two Arrays
- 349. Intersection of Two Arrays
- 如何做科研,选取好的idea
- static的用法
- codeforces825D-Suitable Replacement
- 简单工厂模式
- CSS补充-列表属性
- 349. Intersection of Two Arrays
- 策略路由(PBR)简介
- 【修真院“善良”系列之三】技术面试的时候该怎么样介绍自己?
- hdu 6059 Kanade's trio(字典树+位压缩)
- SDUT-数据结构实验之查找五:平方之哈希表
- 网页防篡改实现技术比较及三大主流产品对比
- POJ 1029 False coin 找假币 模拟
- c++中const有什么作用
- 使用cJSON解析JSON字符串