题目描述

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. sigaction()函数

    sigaction函数 修改信号处理动作(通常在Linux用其来注册一个信号的捕捉函数) int sigaction(int signum, const struct sigaction *act, ...

  2. 7.26T3小游戏

    小游戏 题目描述 有一个简单的小游戏.游戏的主人公是一个勇士,他要穿过一片黑森林,去 解救公主.黑森林的地图可以用一张 V 个点.E 条边的无向图来描述,起点是 1 号点,终点是 V 号点.勇士从起点 ...

  3. PHP 之文件上传类封装

    一.前端代码 <!doctype html> <html lang="en"> <head> <meta charset="UT ...

  4. tmux 入门踩坑记录

    软件安装 sudo apt-get install tmux 1. 分割左右窗口 ^b -> % 运行 tmux 新建一个 tmux 的会话(session),此时窗口唯一的变化是在底部会出现一 ...

  5. Apache Flink - 分布式运行环境

    1.任务和操作链 下面的数据流图有5个子任务执行,因此有五个并行线程. 2.Job Managers, Task Managers, Clients Job Managers:协调分布式运行,他们安排 ...

  6. zabbix :web 界面显示的监控项值为0或者空

    [参考文章]:[错误汇总]zabbix_get 的值一直为 0 1. 问题现象 zabbix 版本:3.4: server 端部署在 192.168.145.134 ,agent 节点部署在 192. ...

  7. vue js select下拉框

    <template> <ul id="select"> <li> <div class="select-head"&g ...

  8. html中如何获取元素在文档中的位置

    html中如何获取元素在文档中的位置 一.总结 一句话总结: $("#elem").offset().top $("#elem").offset().left ...

  9. 黑马vue---40、结合Node手写JSONP服务器剖析JSONP原理

    黑马vue---40.结合Node手写JSONP服务器剖析JSONP原理 一.总结 一句话总结: 服务端可以返回js代码给script标签,那么标签会执行它,并且可带json字符串作为参数,这样就成功 ...

  10. 在阿里云上挂在/data脚本

    在阿里云上加好一块磁盘后,将他分区,挂在在/data,并且设置开机自动挂在/etc/fstab [root@ZHONG-LONG javascripts]# vim mount.sh #!/bin/b ...