要求

  • 给定两个数组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. KubeEdge边缘自治设计原理

    这一篇内容主要是KubeEdge中边缘节点组件EdgeCore的原理介绍. KubeEdge架构-EdgeCore 上图中深蓝色的都是kubeedg自己实现的组件,亮蓝色是k8s社区原生组件.这篇主要 ...

  2. 201871030135-姚辉 实验二 个人项目—《D{0-1} KP》项目报告

    项目 内容 课程班级博客链接 课程班级博客链接 这个作业要求链接 这个作业要求链接 我的课程学习目标 (1)掌握软件项目个人开发流程.(2)掌握Github发布软件项目的操作方法. 这个作业在哪些方面 ...

  3. (十)Docker-V 详解

    1. 作用 挂载宿主机的一个目录. 2. 案例 譬如我要启动一个centos容器,宿主机的/test目录挂载到容器的/soft目录,可通过以下方式指定: # docker run -it -v /te ...

  4. (十一)VMware Harbor 配置管理

    VMware Harbor 配置管理 这篇主要学习一下,项目下的配置管理. 主要功能就是修改项目的访问权限 当将访问权限改为

  5. 图解高性能网络架构:Reactor 和 Proactor

    小林,来了. 这次就来图解 Reactor 和 Proactor 这两个高性能网络模式. 别小看这两个东西,特别是 Reactor 模式,市面上常见的开源软件很多都采用了这个方案,比如 Redis.N ...

  6. RandomForestClassifier参数

    [RandomForestClassifier] 参数 n_estimators : 随机森林中树的个数,即学习器的个数. max_features : 划分叶子节点,选择的最大特征数目 n_feat ...

  7. hdu4499 搜索

    题意:       给你一个棋盘,最大是5*5的,问你最多可以放多少个炮,炮和炮之间不可以相互攻击,这块只的是只能走一步,不存在两个炮中间三个棋子的情况.. 思路:    刚开始想的是把所有的空位置都 ...

  8. 反病毒攻防研究第004篇:利用WinRAR与AutoRun.inf实现自启动

    一.前言 由之前的一系列研究可以发现,为了使得"病毒"能够实现自启动,我也是煞费苦心,采取了各种方式,往往需要编写冗长的代码并且还需要掌握系统底层或注册表的很多知识才可以.而这次我 ...

  9. nginx添加module之threads

    一.安装nginx yum安装nginx 折叠源码 1 2 3 4 5 6 7 8 9 10 11 12 # 添加nginx源 rpm -ivh http://nginx.org/packages/c ...

  10. hdu1054 简单最小顶点覆盖

    题意:       给你一些点,和一些边,如果把一个点安装保护装置,那么与他直接相连的点就可以被保护,题目问的是最少安装多少个点能让所有的点都被保护. 思路:       这是最基础的二分图最少定点覆 ...