Intersection of Two Arrays I II
来源:互联网 发布:windows smb访问 编辑:程序博客网 时间:2024/06/11 10:20
Intersection of Two Arrays I
Given two arrays, write a function to compute their intersection.
Example:
Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2].
返回不允许重复!
class Solution {public: vector<int> intersection(vector<int>& nums1, vector<int>& nums2) { unordered_set<int> st(nums1.begin(),nums1.end()); vector<int> res; for(auto& num:nums2){ if(st.count(num)){ res.push_back(num); st.erase(num); } } return res; }};
Intersection of Two Arrays II
Given two arrays, write a function to compute their intersection.
Example:
Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2, 2].
返回允许重复!
class Solution {public: vector<int> intersect(vector<int>& nums1, vector<int>& nums2) { unordered_map<int,int> mp; for(auto& num:nums1){ ++mp[num]; } vector<int >res; for(auto& num:nums2){ if(mp[num]>0){ res.push_back(num); mp[num]--; } } return res; }};
0 0
- Intersection of Two Arrays I II
- Intersection of Two Arrays II
- Intersection of Two Arrays II
- Intersection of Two Arrays II
- Intersection of Two Arrays II
- Intersection of Two Arrays II
- Intersection of Two Arrays II
- Intersection of Two Arrays II
- Intersection of Two Arrays II
- Intersection of Two Arrays II
- Intersection of Two Arrays II
- Intersection of Two Arrays II
- Intersection of Two Arrays II
- Intersection of Two Arrays II
- Intersection of Two Arrays II
- Intersection of Two Arrays I
- 349. Intersection of Two Arrays I &&II (Java)
- leetcode笔记--Intersection of Two Arrays I & II
- Git——Repository的删除
- LINUX 常用命令汇总
- Am命令用法
- php中curl_multi的应用 同时请求多个url
- 什么是SFP光模块的DDM、DOM和RGD功能?
- Intersection of Two Arrays I II
- 生产环境实战spark (3)Linux CentOS 系统安装 修改主机名 安装JDK
- Maven入门指南(一)
- RunLoop 官方编程手册翻译
- JESD204B传输层
- java 接口方法超时异常处理 设置超时时间
- 属性动画的学习
- Jni基础 (一)Java和c互调
- 从static变量初始化到Spring/Spring boot的工具类静态变量注入