[leetcode]: 349. Intersection of Two Arrays
来源:互联网 发布:debian 软件源配置 编辑:程序博客网 时间:2024/05/23 00:07
1.题目描述
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.
翻译:求两个数组的交集。要求:结果中的元素是唯一的,不限定元素顺序。
2.分析
可以借助c++的set容器,或python的set对象。
3.代码
python
def intersection(self,nums1, nums2): return list(set(nums1)&set(nums2))
c++
vector<int> intersection(vector<int>& nums1, vector<int>& nums2) { unordered_set<int> set1(nums1.begin(), nums1.end()); vector<int> result; for (int n : nums2) { if (set1.erase(n))//元素在set1中 result.push_back(n); } return result;}
0 0
- leetcode 记录 349. Intersection of Two Arrays
- 349.[Leetcode]Intersection of Two Arrays
- [leetcode] 349. Intersection of Two Arrays
- Leetcode Everyday: 349. Intersection of Two Arrays
- leetcode.349. Intersection of Two Arrays
- LeetCode 349. Intersection of Two Arrays
- Leetcode 349. Intersection of Two Arrays
- leetcode 349. Intersection of Two Arrays
- leetcode 349. Intersection of Two Arrays
- LeetCode 349. Intersection of Two Arrays
- 【leetcode】 349. Intersection of Two Arrays
- Leetcode 349. Intersection of Two Arrays
- LeetCode 349. Intersection of Two Arrays
- Leetcode题解 349. Intersection of Two Arrays
- LeetCode - 349. Intersection of Two Arrays
- [Leetcode]349. Intersection of Two Arrays
- 349. Intersection of Two Arrays--LeetCode Record
- LeetCode 349. Intersection of Two Arrays
- C# 结构体与类的区别
- linux查看文件20到30行的三种方法
- Binary Tree Inorder Traversal
- MySQL Replication 的基本知识及简单配置(1)
- 如何读取assets下的文件
- [leetcode]: 349. Intersection of Two Arrays
- boost::function
- js判断当前浏览类型
- Data Structures and Algorithm Analysis in C, Second Edition(《数据结构与算法分析》C语言版 第二版)——Mark Allen Weiss
- poj1664——放苹果(递推)
- swap.c -- 使用指针解决交换函数的问题
- 通过Spring @PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作
- 设计模式讲解与代码实践(二)——生成器
- 当数据传输不方便的时候,可以使用接口回调工具类InterfaceUtils