LeetCode 349. Intersection of Two Arrays
来源:互联网 发布:js怎么使用正则表达式 编辑:程序博客网 时间:2024/06/03 19:36
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.
Code1:
class Solution {public: vector<int> intersection(vector<int>& nums1, vector<int>& nums2) { vector<int> intersection; map<int,int> m; sort(nums1.begin(),nums1.end()); sort(nums2.begin(),nums2.end()); nums1.erase(unique(nums1.begin(),nums1.end()),nums1.end()); nums2.erase(unique(nums2.begin(),nums2.end()),nums2.end()); for(int i=0;i<nums1.size();i++) m[nums1[i]]++; for(int i=0;i<nums2.size();i++) m[nums2[i]]++; for(auto & n:m){ if(n.second==2) intersection.push_back(n.first); } return intersection; }};
Code2:
class Solution {public: vector<int> intersection(vector<int>& nums1, vector<int>& nums2) { unordered_set<int> m(nums1.begin(), nums1.end()); vector<int> res; for (auto a : nums2) if (m.count(a)) { res.push_back(a); m.erase(a); } return res; }};
阅读全文
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
- .pynb文件的打开
- @SuppressWarnings注解用法详解
- 创建进程的两种方式
- 跨浏览器事件的添加与删除
- 计算导论与C语言基础week12期末题
- LeetCode 349. Intersection of Two Arrays
- ubuntu17.04安装搜狗输入法
- Java中@Override的作用
- 机器学习——K-近邻算法
- 隐藏header头部信息中apache和php的版本信息
- 分布式架构实现
- cadence元件库的说明
- 信号报告
- git安装到使用