[刷题] 350 Intersection of Two Arrays
要求
- 给定两个数组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的更多相关文章
- [刷题] 349 Intersection of Two Arrays
查找问题 查找有无(只有键) 元素'a'是否存在 set(集合) 查找对应关系(键值对应) 元素'a'出现了几次 map(字典) set和map的底层实现是红黑树 常见操作 insert() find ...
- 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 ...
- [LeetCode] 349 Intersection of Two Arrays && 350 Intersection of Two Arrays II
这两道题都是求两个数组之间的重复元素,因此把它们放在一起. 原题地址: 349 Intersection of Two Arrays :https://leetcode.com/problems/in ...
- 26. leetcode 350. Intersection of Two Arrays II
350. Intersection of Two Arrays II Given two arrays, write a function to compute their intersection. ...
- 【leetcode】350. Intersection of Two Arrays II
problem 350. Intersection of Two Arrays II 不是特别明白这道题的意思,例子不够说明问题: 是按顺序把相同的元素保存下来,还是排序,但是第二个例子没有重复... ...
- 【一天一道LeetCode】#350. Intersection of Two Arrays II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given t ...
- [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 ...
- [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 ...
- leetcode349 350 Intersection of Two Arrays & II
""" Intersection of Two Arrays Given two arrays, write a function to compute their in ...
随机推荐
- 更改当前目录--cd
pwd 显示当前所在的目录路径 cd ../ 回到上一层目录 cd ../../ 回到上上层目录 cd / 回到根目录 cd ~ 回到当前用户的根目录 c ...
- [Fundamental of Power Electronics]-PART I-5.不连续导电模式-5.4 总结与重点
5.4 总结与重点 基本的buck,boost以及buck-boost电路的特点总结在表5.2中.其中给出了\(K_{crit}(D)\)的表达式,CCM和DCM下的变换比,以及DCM下二极管导通占空 ...
- 2021年春软件工程"助教团队"成员介绍
2021年春软件工程 助教团队 成员介绍 项目 内容 这个作业属于那个课程 2021春季学期软件工程(罗杰.任健) 这个作业的要求在哪里 团队介绍 姓名 照片 个人介绍 分工 刘Q 本团队的PM,和助 ...
- Spring01-模块划分
Test:pring的单元测试模块 Spring-test-4.0.0.RELEASE Core Continer:核心容器(IOC):黑色代表这部分的功能由哪些jar包组成:要使用这个部分的完整功能 ...
- Ducci Sequence UVA - 1594
A Ducci sequence is a sequence of n-tuples of integers. Given an n-tuple of integers (a1,a2,···,an ...
- Day13_66_sleep()方法 (二)
sleep()方法 (二) * 因为sleep()是静态方法,所以用 线程对象名.sleep() 和 Thread.sleep()是一样的意思,用线程对象名调用并不等于让线程对象名所指向的线程阻塞,而 ...
- 01-Verilog基本语法元素
不知道能不能更新完,毕竟咱学校计院对硬件向来不太重视,现在对竞赛也不咋地重视了,也不加分,也没啥用.嘛,就随便写写玩玩吧. 一只狸无聊的时候对Verilog的业余描述笔记:以<Verilog数字 ...
- SpringCloud之远程调用OpenFeign和Ribbon
Ribbon.Feign和OpenFeign的区别 SpringCloudAlibaba微服务实战教程系列 Spring Cloud 微服务架构学习记录与示例 一 简介 Feign是Netflflix ...
- 【运维--系统】nacos介绍和安装
目录: 简介 安装java 安装mysql 安装nacos 附录 简介 Nacos 致力于帮助您发现.配置和管理微服务.Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现.服务配置.服 ...
- POJ 2752 同一个串的前后串
题解东北赛回来再补 #include<stdio.h> #include<string.h> int next[500000]; int ans[500000]; char s ...