要求

  • 给定两个数组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. 四、MYSQL数据练习题

    我的MYSQL版本是mysql-5.7.24-winx64,每天练习5道习题. 如果有错误或者更优的解决方法,欢迎大家指出,谢谢!! 一.测试表格 --1.学生表Student(Sid,Sname,S ...

  2. Redis扩展数据类型详解

    在Redis中有5种基本数据类型,分别是String, List, Hash, Set, Zset.除此之外,Redis中还有一些实用性很高的扩展数据类型,下面来介绍一下这些扩展数据类型以及它们的使用 ...

  3. [DFS]特殊的质数肋骨

    特殊的质数肋骨 时间限制:1000MS----内存限制:256000KB 题目描述 农民约翰母牛总是产生最好的肋骨. 你能通过农民约翰和美国农业部标记在每根肋骨上的数字认出它们. 农民约翰确定他卖给买 ...

  4. GO-02-helloworld

    package main /** package main表示一个可独立执行的程序,每个Go应用程序都需要包含一个名为main的包, 并且该包包含一个叫main()的函数(该函数是Go可执行程序的执行 ...

  5. 我最近做了一个react的后台管理系统,用于快速创建后台项目模板

    react-ant-admin 此框架使用与二次开发,前端框架使用react,UI框架使用ant-design,全局数据状态管理使用redux,ajax使用库为axios.用于快速搭建中后台页面.欢迎 ...

  6. Day11_55_在Map集合中使用泛型

    在Map集合中使用泛型 ``` import java.util.HashMap; import java.util.Iterator; import java.util.Map; import ja ...

  7. 使用numba加速python科学计算

    技术背景 python作为一门编程语言,有非常大的生态优势,但是其执行效率一直被人诟病.纯粹的python代码跑起来速度会非常的缓慢,因此很多对性能要求比较高的python库,需要用C++或者Fort ...

  8. jQuery+AJAX实现纯js分页功能

    使用jQuery的AJAX技术,在bootstrap的框架下搭建的纯js分页 bootstrap作为Twitter推的一款前端框架,效果个人还是觉得很不错的.这次只是拿来作为网页元素的css样式表使用 ...

  9. $.ajax()——超时设置

    getAjax: function (method, apiUrl, options, callback) { var xhr = $.ajax({ type: method, url: apiUrl ...

  10. Average Score39届亚洲赛牡丹江站A题

    题意:       A班有n个人,B班有m个人,然后现在给你n-1个A班人的成绩,和m个B班人的成绩,然后题目要求求出A班剩下的没给成绩那个人的成绩范围,要求是这个人从A班转到B班后能使A,B的平均分 ...