Given two arrays, write a function to compute their intersection.
Example 1:
Input: nums1 = [1,2,2,1], nums2 = [2,2]
Output: [2,2]
Example 2:
Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
Output: [4,9]
Note:
Each element in the result should appear as many times as it shows in both arrays.
The result can be in any order.
Solution:
#include <iostream>
#include <vector>
#include <map>
using namespace std;
class Solution{
public:
vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {
map<int, int> record;
for( int i = 0; i < nums1.size(); i++)
record[nums1[i]] ++;
vector<int> resultVector;
for( int i = 0; i < nums2.size(); i++)
if(record[nums2[i]] > 0) {
resultVector.push_back(nums2[i]);
record[nums2[i]]--;
}
return resultVector;
}
};
总结:
注意和349题的区别。本题用了map 结构。