题目描述

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. python输出的高亮显示

    一.语法 1.书写格式 开头部分: \033[显示方式;前景色;背景色m 结尾部分: \033[0m   例如:\033[31;1m测试的文字部分\033[0m   注意: 开头部分的三个参数:显示方 ...

  2. 一步一步配置AWS ELB Https证书

    第一步:生成CSR 要配置证书,我们首先需要创建一个CSR来向证书提供商申请证书.这个过程我们可以通过IIS中的工具来生成. 然后需要填写如下信息: 下一步后选择文件名后我们就可以创建出CSR 文件了 ...

  3. 爬虫之解析库BeautifulSoup

    介绍 Beautiful Soup是python的一个库,最主要的功能是从网页抓取数据.官方解释如下: Beautiful Soup提供一些简单的.python式的函数用来处理导航.搜索.修改分析树等 ...

  4. P5149 会议座位

    P5149 会议座位 题意: 其实还是求逆序对数. 解法: 用离散化统计每个数,再用树状数组求逆序对. CODE: #include<iostream> #include<cstdi ...

  5. awk、grep、sed

    awk.grep.sed是linux操作文本的三大利器,也是必须掌握的linux命令之一.三者的功能都是处理文本,但侧重点各不相同,其中属awk功能最强大,但也最复杂.grep更适合单纯的查找或匹配文 ...

  6. 数据库安装后无法访问且mysql重启报错的解决方法

    数据库安装后无法访问,mysql重启报错: 或报错:MySQL is running but PID file could not be found 解决方法: 第一种方法:看磁盘是否已满:df –h ...

  7. arcgis python 随机取部分数据

    # -*- coding: cp936 -*- import arcpy import os import ylpy import random def main(): num=ylpy.getCou ...

  8. SQL-W3School-高级:SQL RIGHT JOIN 关键字

    ylbtech-SQL-W3School-高级:SQL RIGHT JOIN 关键字 1.返回顶部 1. SQL RIGHT JOIN 关键字 RIGHT JOIN 关键字会右表 (table_nam ...

  9. .IllegalArgumentException: Mapped Statements collection does not contain 异常一例【我】

    更新代码后发现几乎所有的sql查询都报错,类似下面: java.lang.RuntimeException: org.mybatis.spring.MyBatisSystemException: ne ...

  10. thymeleaf中img标签图片src路径问题

    转载自:解决java - Thymeleaf conditional img src 正确写法. <img class="layui-nav-img" th:src=&quo ...