要求

  • 给定两个数组nums,求两个数组交集
  • 输出结果与元素在两个数组中出现的次数一致
  • 不考虑输出结果的顺序

举例

  • nums1=[1,2,2,1]
  • nums2=[2,2]
  • 结果:[2,2]

思路

  • 使用map,记录nums1中元素及其出现次数
  • 遍历nums2,将重复元素放入结果,同时将该元素出现次数减1

实现

 1 class Solution{
2 public:
3 vector<int> intersect(vector<int>& nums1, vector<int>& nums2){
4 map<int,int> record;
5 for( int i = 0 ; i < nums1.size() ; i ++ )
6 record[nums1[i]] ++;
7
8 vector<int> resultVector;
9 for( int i = 0 ; i < nums2.size() ; i ++ )
10 if( record[nums2[i]] > 0 ){
11 resultVector.push_back( nums2[i] );
12 record[nums2[i]]--;
13 }
14 return resultVector;
15 }
16 };

[刷题] 350 Intersection of Two Arrays的更多相关文章

  1. [刷题] 349 Intersection of Two Arrays

    查找问题 查找有无(只有键) 元素'a'是否存在 set(集合) 查找对应关系(键值对应) 元素'a'出现了几次 map(字典) set和map的底层实现是红黑树 常见操作 insert() find ...

  2. LeetCode Javascript实现 169. Majority Element 217. Contains Duplicate(两个对象比较是否相等时,如果都指向同一个对象,a==b才是true)350. Intersection of Two Arrays II

    169. Majority Element /** * @param {number[]} nums * @return {number} */ var majorityElement = funct ...

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

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

  4. 26. leetcode 350. Intersection of Two Arrays II

    350. Intersection of Two Arrays II Given two arrays, write a function to compute their intersection. ...

  5. 【leetcode】350. Intersection of Two Arrays II

    problem 350. Intersection of Two Arrays II 不是特别明白这道题的意思,例子不够说明问题: 是按顺序把相同的元素保存下来,还是排序,但是第二个例子没有重复... ...

  6. 【一天一道LeetCode】#350. Intersection of Two Arrays II

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

  7. [LeetCode] 350. Intersection of Two Arrays II 两个数组相交之二

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

  8. [LeetCode] 350. Intersection of Two Arrays II 两个数组相交II

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

  9. leetcode349 350 Intersection of Two Arrays & II

    """ Intersection of Two Arrays Given two arrays, write a function to compute their in ...

随机推荐

  1. 使用Drone构建Docker映像

    使用Drone构建Docker映像 实践所用软件: Git Gogs Drone Docker 私有镜像仓库 实践链接:https://www.katacoda.com/courses/cicd/bu ...

  2. [Fundamental of Power Electronics]-PART I-5.不连续导电模式-5.1 DCM来源和模式边界

    引子: 当使用电流单向和/或电压单向半导体开关实现DC-DC变换器的理想开关时,可能会出现一种或多种被称为不连续导电模式(DCM)的新工作模式.当电感电流或电容电压的纹波大到足以导致所施加的开关电流或 ...

  3. IDEA如何在一个项目空间下管理多个项目?

    用过Eclipse和IDEA编程工具都知道,Eclipse创建新项目时都是在同一项目空间下,而IDEA一个项目空间只能有一个项目,创建项目时会创建.idea文件. 所以每次创建完项目或者打开另一个项目 ...

  4. 数据库MySQL六

    介绍什么是JDBC JAVA SE也有 提高综合篇 JDBC(Java Database Connectivity) :java和数据库的连接技术,sun公司推出的一套java应用程序访问数据库的技术 ...

  5. 「一站式」兼容所有云厂商文件存储Spring Boot 实现

    背景 在互联网发展的今天,近乎所有的云厂商都提供对象存储服务.一种海量.安全.低成本.高可靠的云存储服务,适合存放任意类型的文件.容量和处理能力弹性扩展,多种存储类型供选择,全面优化存储成本. 当我们 ...

  6. BLE链路层状态机初探

    状态机 BLE链路层把所有的功能放到五种不同的状态中,在不同的状态分别执行不同的功能. 一般来说,BLE设备大致有这么几种状态:空闲,广播,扫描,发起连接和连接成功. 广播和扫描是相对应的,一个设备广 ...

  7. Node.js/Vue.js使用jsSHA库进行SHA1/2/3加密

    1 概述 jsSHA是一个用JS+TS实现完整SHA系列加密算法的加密库,包括: SHA1 SHA-224/256/384/512 SHA3-224/256/384/512 SHAKE128/256 ...

  8. 004-Java中的运算符

    @ 目录 一.运算符 一.分类 二.算数运算符 三.关系运算符 四.逻辑运算符 五.赋值运算符 六.条件运算符(三目运算符) 七.+运算符 一.运算符 一.分类 二.算数运算符 加  $+$ 减  $ ...

  9. 终于可以像使用 Docker 一样丝滑地使用 Containerd 了

    有追求的工程师一般都是有技术洁癖的,云原生的世界更是如此,Kubernetes虽然制定了容器运行时接口(CRI)标准,但早期能用的容器运行时只有Docker,而Docker 又不适配这个标准,于是给 ...

  10. git 配置ssh

    git 配置ssh 生成一个个人账号/邮箱的sshkey ssh-keygen -t rsa -C "youremail@yourcompany.com" -f ~/.ssh/XX ...