【LeetCode】349. Intersection of Two Arrays
来源:互联网 发布:冰与火之歌世界观知乎 编辑:程序博客网 时间:2024/06/06 05:51
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使用SET。
class Solution {public: vector<int> intersection(vector<int>& nums1, vector<int>& nums2) { set<int>s1,s2; vector<int> ans; for(int i=0;i<nums1.size();i++)s1.insert(nums1[i]); for(int i=0;i<nums2.size();i++)s2.insert(nums2[i]); set<int>::iterator it; for(it=s1.begin();it!=s1.end();++it){ if(s2.count(*it))ans.push_back(*it); } return ans; }};
方法2:使用MAP
class Solution {public: vector<int> intersection(vector<int>& nums1, vector<int>& nums2) { map<int,bool>mp; vector<int> ans; for(int i=0;i<nums1.size();i++)mp[nums1[i]]=true; for(int i=0;i<nums2.size();i++){ if(mp[nums2[i]]){ ans.push_back(nums2[i]); mp.erase(nums2[i]); } } return ans; }};
阅读全文
0 0
- leetcode 记录 349. Intersection of Two Arrays
- 349.[Leetcode]Intersection of Two Arrays
- [leetcode] 349. Intersection of Two Arrays
- Leetcode Everyday: 349. Intersection of Two Arrays
- leetcode.349. Intersection of Two Arrays
- LeetCode 349. Intersection of Two Arrays
- Leetcode 349. Intersection of Two Arrays
- leetcode 349. Intersection of Two Arrays
- leetcode 349. Intersection of Two Arrays
- LeetCode 349. Intersection of Two Arrays
- 【leetcode】 349. Intersection of Two Arrays
- Leetcode 349. Intersection of Two Arrays
- LeetCode 349. Intersection of Two Arrays
- Leetcode题解 349. Intersection of Two Arrays
- LeetCode - 349. Intersection of Two Arrays
- [Leetcode]349. Intersection of Two Arrays
- 349. Intersection of Two Arrays--LeetCode Record
- LeetCode 349. Intersection of Two Arrays
- JavaScript学习---4
- 奇淫技巧____MyEclipse2017破解教程
- Jupyter notebook 配置
- left join 和 left outer join 的区别
- phpcms 数据库操作总结
- 【LeetCode】349. Intersection of Two Arrays
- Java 利用脚本API运行Groovy脚本的方式
- Android实现长时间等待界面
- MySQL触发器详解
- Linux学习笔记(4)-磁盘管理
- 数据库知识点
- 利用php://filter写入一句话及注意事项
- 在Visual Studio平台上的应用libiconv库进行字符集转换的范例
- 基于SEDA的异步框架设计与实现