[刷题] 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 ...
随机推荐
- [SpringCloud教程]2. 版本选型和项目搭建
Spring Cloud Alibaba 版本选型 建议先选择Spring Cloud Alibaba的大版本,方便兼容 选择 Spring Cloud Alibaba 大版本 访问链接,找到标题&q ...
- 3w 字长文爆肝 Java 基础面试题!太顶了!!!
hey guys ,这不是也到了面试季了么,cxuan 又打算重新写一下 Java 相关的面试题,先从基础的开始吧,这些面试题属于基础系列,不包含多线程相关面试题和 JVM 相关面试题,多线程和 JV ...
- 击鼓传花联想到了Java设计模式:责任链模式
目录 应用场景 简单示例 责任链模式 定义 意图 主要解决问题 何时使用 优缺点 击鼓传花的故事 应用场景 http web请求处理,请求过来后将经过转码.解析.参数封装.鉴权等一系列的处理(责任), ...
- JMeter线程组编辑区揭秘
线程组编辑区如下: 有点复杂,但是慢慢看下来,还是比较容易理解. Name 带有业务含义的名字. Comments 线程组的备注说明. Action to be taken after a Sampl ...
- C++ new和delete运算符得简单使用
NEW C++ 中的new运算符用来分配内存,和c语言中得malloc有相似得功能. 使用new为当个元素开辟内存空间,并返回地址 typeName *pointer_name =new typeNa ...
- TEX Quotes UVA-272
TEX is a typesetting language developed by Donald Knuth. It takes source text together with a few ...
- OpenCV 之 空间刚体变换
刚体就是 "刚性物体",它在运动过程中,内部各质点间的相对位置不会改变,也即 每两个质点间的距离 保持不变 假设刚体内任意两个质点,坐标分别为 $(x_1, y_1, z_1)$ ...
- linux gcc命令参数
gcc命令参数笔记 1. gcc -E source_file.c -E,只执行到预处理.直接输出预处理结果. 2. gcc -S source_file.c -S,只执行到汇编,输出汇编代码. 3. ...
- 阿里云《nginx服务器配置SSL证书》 配置参数
server { listen 443; server_name demo.shengruijt25.com; ssl on; root html; index index.html index.ht ...
- RF-日期时间拼接(20191024_195355)
*** Test Cases *** testGetTime @{time}= Get Time year month day hour min sec ${sDate}= Catenate SEPA ...