Hash Table -- Leetcode problem349. Intersection of Two Arrays
来源:互联网 发布:foxmail邮件导入到mac 编辑:程序博客网 时间:2024/06/05 18:53
- 描述:Given two arrays, write a function to compute their intersection.
Example:
Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2].
Note:
Each element in the result must be unique.
The result can be in any order.
- 分析:这道题是找出两个数组中的重复元素之后输出,要求输出无重复。
- 思路一:用unordered_map来进行比较操作
class Solution {public: vector<int> intersection(vector<int>& nums1, vector<int>& nums2) { vector<int> my_vec; unordered_map<int, int> my_map; for (int i = 0; i < nums1.size(); i++) { my_map[nums1[i]] = 0; } for (int i = 0; i < nums2.size(); i++) { if (my_map.find(nums2[i]) != my_map.end()) { my_vec.push_back(nums2[i]); my_map.erase(nums2[i]); } } return my_vec;}};
阅读全文
0 0
- Hash Table -- Leetcode problem349. Intersection of Two Arrays
- Hash Table -- Leetcode problem350. Intersection of Two Arrays II
- Leetcode - Hash Table - 349+350 Intersection of Two Arrays(快速返回两数组重叠部分)
- LeetCode-Hash-349. Intersection of Two Arrays && 350. Intersection of Two Arrays II
- [LeetCode] Intersection of Two Arrays
- LeetCode Intersection of Two Arrays
- LeetCode:Intersection of Two Arrays
- 【leetcode】Intersection of Two Arrays
- leetcode Intersection of Two Arrays
- 【leetcode】Intersection of Two Arrays
- leetcode:Intersection of Two Arrays
- [Leetcode]Intersection of Two Arrays
- LeetCode:Intersection of Two Arrays
- LeetCode: Intersection of Two Arrays
- leetcode ----Intersection of Two Arrays
- LeetCode Intersection of Two Arrays
- [Leetcode] Intersection of Two Arrays
- LEETCODE--Intersection of Two Arrays
- 【知了堂学习笔记】myBatis实现增删查改
- linux:破解navicat
- Tablayout
- Linux:mysql数据库的热备份,主从数据库搭建
- java集合list
- Hash Table -- Leetcode problem349. Intersection of Two Arrays
- PHP实现阶乘的原理与代码分析
- 标准C语言 IO
- 判断Bigdecimal类型是否等于0的方法
- java集合Map
- Thrift结构分析及增加取客户端IP功能实现
- mac 下使用 iTerm 终端工具无法使用 alt+b 和 alt+f 快捷键的解决方法
- 那些我在Android开发中所喜爱的Kotlin特性
- 简单的算法题,包括1.打印100——200之间的的素数2.输出乘法口诀表3.判断1000年——2000年之间的闰年,给出完整代码