题目:
Given two arrays, write a function to compute their intersection.
Example: Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2, 2].
Note:
Each element in the result should appear as many times as it shows in both arrays.The result can be in any order. 思路:本题与前一篇博客:Intersection of Two Arrays有些许的不同,前一篇博客,是找到交叉线,不保留重复值,本篇博客是找到交叉项,保留重复值。本题采用find函数
代码:
class Solution { public: vector<int> intersect(vector<int>& nums1, vector<int>& nums2) { vector<int> res; for(int j = 0;j<nums1.size();j++) { vector<int>::iterator itr = find(nums2.begin(),nums2.end(),nums1[j]); if(itr!=nums2.end()) { res.push_back(*itr); nums2.erase(itr); } } return res; } };
