349. Intersection of Two Arrays (set)
来源:互联网 发布:淘宝详情页切图大小 编辑:程序博客网 时间:2024/05/21 01:42
https://leetcode.com/problems/intersection-of-two-arrays/description/
题目: 求集合的交集。
第一种解法,直接用set集合。
class Solution {public: vector<int> intersection(vector<int>& nums1, vector<int>& nums2) { set<int>temp(nums1.begin(),nums1.end()),temp1; vector<int>result; int len1=nums1.size(),len2=nums2.size(); for(int x=0;x<len2;x++) if(temp.count(nums2[x])&&!temp1.count(nums2[x])) temp1.insert(nums2[x]),result.push_back(nums2[x]); return result; }};
第二种,使用 set_intersection函数。
class Solution {public: vector<int> intersection(vector<int>& nums1, vector<int>& nums2) { set<int>temp1(nums1.begin(),nums1.end()),temp2(nums2.begin(),nums2.end()),temp3; set_intersection(temp1.begin(), temp1.end(), temp2.begin(), temp2.end(), inserter(temp3, temp3.begin())); vector<int>result(temp3.begin(),temp3.end()); return result; }};
阅读全文
0 0
- 349. Intersection of Two Arrays (set)
- LeetCode 349. Intersection of Two Arrays 350. Intersection of Two Arrays II set map使用 ****
- 349.leetcode Intersection of Two Arrays(easy)[数组 交集 set]
- 349. Intersection of Two Arrays
- 349. Intersection of Two Arrays[][
- 349. Intersection of Two Arrays
- 349. Intersection of Two Arrays
- 349. Intersection of Two Arrays
- 349. Intersection of Two Arrays
- 349. Intersection of Two Arrays
- 349. Intersection of Two Arrays
- 349. Intersection of Two Arrays
- 349. Intersection of Two Arrays
- 349. Intersection of Two Arrays
- 349. Intersection of Two Arrays
- 349. Intersection of Two Arrays
- 349. Intersection of Two Arrays
- 349. Intersection of Two Arrays
- 二叉树计算题总结
- 利用反射得到属性的值
- SLAM基础技术点之基于计算机视觉求解相机姿态变化的方法汇总
- 命令模式
- IO--实例操作
- 349. Intersection of Two Arrays (set)
- 面试
- 数据仓库的架构-参考
- void类型指针声明
- Android 抓日志的各种命令
- windows服务器中文乱码
- material-ui 问题select Field Date Picker点击未响应 处理方法
- IDEA 快捷键使用(持续更新)
- Unix网络编程学习日记(五):基于epoll与管道的socket客户端