LeetCode 349. Intersection of Two Arrays
来源:互联网 发布:网络电视遥控器没反应 编辑:程序博客网 时间:2024/06/06 09:47
Intersection of Two Arrays
题目描述:
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.
题目思路:
1.对nums1中对数字做标记。
2.遍历nums2数组,记录被标记过对元素,注意不能出现重复元素。
3.返回记录对元素结果。
题目代码:
class Solution {public: vector<int> intersection(vector<int>& nums1, vector<int>& nums2) { map<int,int>table; vector<int>ans; for(int i = 0; i < nums1.size(); i++){ table[nums1[i]]++; } for(int i = 0; i < nums2.size(); i++){ if(table[nums2[i]] && !count(ans.begin(),ans.end(),nums2[i])) ans.push_back(nums2[i]); } return ans; }};
阅读全文
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
- CSS 转义字符对照表
- Qt Creator中如何设置代码的背景色和字体
- C# 点类程序
- 各种一句话木马大全
- echarts小贴纸之图表标注字体格式设置
- LeetCode 349. Intersection of Two Arrays
- Linux下ps命令的详细使用方法
- 一丢丢openlayers3学习记录【1】
- nodejs笔记
- windows下python安装dlib机器学习开源库
- Linux服务器安全防护
- SSM框架Spring+SpringMVC+MyBatis——详细整合教程
- droputout使用
- java实现人脸识别源码【含测试效果图】——实体类(Users)