Leetcode 349 Intersection of Two Arrays
来源:互联网 发布:手机淘宝领天猫积分 编辑:程序博客网 时间:2024/06/05 10:46
Leetcode 349 Intersection of Two Arrays
#include <vector>#include <algorithm>#include <set>using namespace std;class Solution {public: vector<int> intersection(vector<int>& nums1, vector<int>& nums2) { vector<int> result; for(int num : nums1) { if(find(nums2.begin(),nums2.end(),num) != nums2.end()) result.push_back(num);//在num2中将其删掉就不用再去重了…… } if(!result.empty()) { sort(result.begin(),result.end()); auto iter = unique(result.begin(),result.end());//不去重不排序当然会快,笨蛋。 if(iter != result.end()) result.erase(iter,result.end()); } return result; } vector<int> intersection2(vector<int>& nums1, vector<int>& nums2) { vector<int> result; unordered_set<int> s(nums1.begin(),nums1.end());//element is unique~ ATTENTION for(int num : nums2) { if(s.find(num) != s.end()) { result.push_back(num); s.erase(num); } } return result; }};
阅读全文
0 0
- Leetcode-349 Intersection of Two Arrays
- leetcode 349 Intersection of Two Arrays C++
- LeetCode 349 Intersection of Two Arrays
- LeetCode-349&350.Intersection of Two Arrays
- LeetCode-349 Intersection of Two Arrays
- leetcode-Intersection of Two Arrays-349
- 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(java)
- Leetcode 349 Intersection of Two Arrays
- LeetCode No.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 122 Best Time to Buy and Sell Stock II
- linux下动态库(共享库)
- 使用foreach操作数组
- HDU 5977 树的分治+子集枚举
- 1560 扔人游戏
- Leetcode 349 Intersection of Two Arrays
- 查看linux中的TCP连接数
- 地图集web项目_技术学习(一)_前后端数据传输(ajax)
- Leetcode 350 Intersection of Two Arrays II
- Matlab学习笔记(9)——textread函数
- C++中for循环的5种语法
- codevs 1021 玛丽卡 图论:最短路
- Mac 安装JDK 配置环境变量
- Linux查看和剔除当前登录用户