LeetCode 349. Intersection of Two Arrays
来源:互联网 发布:mui.js下载 编辑:程序博客网 时间:2024/06/07 05:39
When i first see this question, I don’t know how to complete it. I am not that familiar with c++’s vector or something like that. After looked up in the website, i know i should use set.
c++:
class Solution {public: vector<int> intersection(vector<int>& nums1, vector<int>& nums2) { set<int> a; vector <int> rslt; for(int i=0; i<nums1.size(); i++){ a.insert(nums1[i]); } for(int i=0; i<nums2.size(); i++){ if(a.find(nums2[i])!=a.end()){ a.erase(nums2[i]); rslt.push_back(nums2[i]); } } return rslt; }};
- set insert: set.insert()
- set.find: if not found, it will return set.end()
- vector insert: vector.push_back()
- vector.insert(position, value)
- vector length: vector.size()
Java
can use HashSet<\integer>, see Link:
http://blog.csdn.net/mebiuw/article/details/51448806
Python
Python is a little easier:
class Solution(object): def intersection(self, nums1, nums2): """ :type nums1: List[int] :type nums2: List[int] :rtype: List[int] """ relt=set(); for num in nums1: if num in nums2: relt.add(num) return list(relt)
- set.add(i)
- list.append(i)
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
- JavaScript对象之正则表达式-RegExp
- 观光旅游(floyed最小环)
- Android使用HttpClient请求服务器代码优化版
- Android沉浸式状态栏
- 编写类String的构造函数、析构函数和赋值函数
- LeetCode 349. Intersection of Two Arrays
- JMS入门(三)--Queue的使用
- win10下安装centos7双系统之开机系统引导问题
- QT快捷键
- CodeForces 612C Replace To Make Regular Bracket Sequence
- CSS user-select
- SQL中的ISNULL的用法
- 学习Lemon OA系统:一、流程定义
- JS控制静态页面之间传递参数获取参数并应用(window.location)