题目描述

Given two arrays, write a function to compute their intersection.

Example 1:

Input: nums1 = [1,2,2,1], nums2 = [2,2]
Output: [2]

Example 2:

Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
Output: [9,4]

Note:

  • Each element in the result must be unique.
  • The result can be in any order.

参考答案

 class Solution {
public:
vector<int> intersection(vector<int>& nums1, vector<int>& nums2) { unordered_set<int> hashset(nums1.begin(),nums1.end());
vector<int> res;
for(auto & i :nums2){ // 提取所有的2
if(hashset.count(i)){ // 拿2去找 1
res.push_back(i);
hashset.erase(i); // 比对完了,就把1里面的给删除吧
}
}
return res;
}
};

LC 349. Intersection of Two Arrays的更多相关文章

  1. [LeetCode] 349 Intersection of Two Arrays && 350 Intersection of Two Arrays II

    这两道题都是求两个数组之间的重复元素,因此把它们放在一起. 原题地址: 349 Intersection of Two Arrays :https://leetcode.com/problems/in ...

  2. LeetCode Javascript实现 283. Move Zeroes 349. Intersection of Two Arrays 237. Delete Node in a Linked List

    283. Move Zeroes var moveZeroes = function(nums) { var num1=0,num2=1; while(num1!=num2){ nums.forEac ...

  3. [LeetCode] 349. Intersection of Two Arrays 两个数组相交

    Given two arrays, write a function to compute their intersection. Example 1: Input: nums1 = [1,2,2,1 ...

  4. LeetCode 349. Intersection of Two Arrays

    Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...

  5. 349. Intersection of Two Arrays

    Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...

  6. 15. leetcode 349. Intersection of Two Arrays

    Given two arrays, write a function to compute their intersection. Example: Given nums1 = [1, 2, 2, 1 ...

  7. 【leetcode】349. Intersection of Two Arrays

    Given two arrays, write a function to compute their intersection. Example: Given nums1 = [1, 2, 2, 1 ...

  8. Add to List 349. Intersection of Two Arrays

    Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...

  9. 【一天一道LeetCode】#349. Intersection of Two Arrays

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given t ...

随机推荐

  1. libimobiledevice

    #### 安装与卸载 ```bashideviceinstaller -i xxx.ipa # 安装ideviceinstaller -u [bundleID] # 卸载ideviceinstalle ...

  2. docker 安装redis 注意事项

    一. redis配置文件修改(重要) ~/redis.conf 中daemonize=NO.非后台模式,如果为YES 会的导致 redis 无法启动,因为后台会导致docker无任务可做而退出. 三  ...

  3. Reconnect due to socket error java.nio.channels.ClosedChannelException

    storm整合kafka后出现如下异常: 错误原因:有部分kafka服务器连接不上导致,检查一下是不是每个kafka都能连接到(有的kafka配置使用的是host,记得配置相同的环境) 造成异常代码段 ...

  4. 重读APUE(8)-进程、进程组、会话

    进程: 是系统中一段程序执行的实体,也是资源分配和调度的基本单位: 进程组: 为了方便管理多个进程,可以将多个进程加入到一个进程组内: 每个进程都属于一个进程组,但是同一个进程组内可以有多个进程: 每 ...

  5. git提交异常 fatal: LF would be replaced by..

    git提交代码时,一直报出“fatal: LF would be replaced by CRLF in (文件名)”的异常,导致代码提交不到远程仓储.其实是,不同系统对换行符的解释不同导致的. 方法 ...

  6. Swiper轮播插件使用

    前文 Swiper是纯javascript打造的滑动特效插件,面向手机.平板电脑等移动终端,能实现触屏焦点图.触屏Tab切换.触屏多图切换等常用效果.                 归根到此,Swi ...

  7. 阶段5 3.微服务项目【学成在线】_day02 CMS前端开发_12-webpack研究-webpack安装

    npm默认安装配置的路径配置在nodejs的node_modules目录 j加上 -g 就是全局安装 后面只写webpack默认安装的是最新版本 指定版本号 视频中建议指定版本号进行安装

  8. [转]Android 应用自动更新及6.0,7.0,8.0适配安装

    原贴:https://www.jianshu.com/p/ea42040c7ace 原贴:https://www.jianshu.com/p/ea42040c7ace 原贴:https://www.j ...

  9. Go 微服务架构Micro相关概念理解

    Micro是一个微服务框架(或者说是工具集):提供了各类组件,解决微服务架构中的不同问题,服务监控.服务发现.熔断机制,负载均衡等等,自己一个个解决这些问题几乎不可能,这时候就需要借助go-micro ...

  10. Python的数据类型与数据结构

    Python的数据类型与数据结构 数据类型分为: 整数型 :数字的整数 浮点型: 数字带小数 字符串: 用 ‘’ 或者 “” 引用的任意文本 布尔型:只有 True 和 False 数据结构分为: 列 ...