Leetcode 350 Intersection of Two Arrays II
2017-10-03 11:18
417 查看
Leetcode 350 Intersection of Two Arrays II
#include <vector> #include <algorithm> #include <map> using namespace std; class Solution { public: vector<int> intersect(vector<int>& nums1, vector<int>& nums2) { vector<int> result; vector<int>::iterator iter; for(iter = nums2.begin();iter != nums2.end();iter ++) { vector<int>::iterator finditer = find(nums1.begin(),nums1.end(),*iter); if(finditer != nums1.end()) { result.push_back(*iter); nums1.erase(finditer); if(nums1.empty()) return result; } } return result; } vector<int> intersect2(vector<int>& nums1, vector<int>& nums2) { vector<int> result; unordered_map<int,int> hash; for(int i = 0;i < nums1.size();i ++) hash[nums1[i]] ++; for(int i = 0;i < nums2.size();i ++) { if(hash[nums2[i]] > 0) { result.push_back(nums2[i]); hash[nums2[i]] --; } } return result; } };
相关文章推荐
- LeetCode 350:Intersection of Two Arrays II
- leetcode-350-Intersection of Two Arrays II
- Leetcode #350 Intersection of Two Arrays II
- Python [Leetcode 350]Intersection of Two Arrays II
- LeetCode 350 Intersection of Two Arrays II
- LeetCode解题笔记350 Intersection of Two Arrays II
- leetcode350——Intersection of Two Arrays II
- leetcode_350 Intersection of Two Arrays II
- [LeetCode] 349 Intersection of Two Arrays && 350 Intersection of Two Arrays II
- LeetCode(350)Intersection of Two Arrays II
- 350_LeetCode_350 Intersection of Two Arrays II 题解
- leetcode350~Intersection of Two Arrays II
- LeetCode-350 Intersection of Two Arrays II
- <LeetCode OJ> 349 / 350 Intersection of Two Arrays(I / II)
- leetcode_c++:哈希:Intersection of Two Arrays II(350)
- leetcode 350 Intersection of two arrays II
- leetcode 350 Intersection of Two Arrays II C++
- leetcode note--leetcode 350 Intersection of Two Arrays II
- Leetcode_350_Intersection of Two Arrays II
- Leetcode 350 Intersection of Two Arrays II