原文题目:
350. Intersection of Two Arrays II
读题:
这个和349. Intersection of Two Arrays类似,用C++实现,不用set,直接用入参的vector,稍微修改下
C++代码:
class Solution { public: vector<int> intersect(vector<int>& nums1, vector<int>& nums2) { vector <int> result; vector <int>::iterator ite1; vector <int>::iterator ite2; int i =0; int j = 0; sort(nums1.begin(),nums1.end()); sort(nums2.begin(),nums2.end()); ite1 = nums1.begin(); ite2 = nums2.begin(); while(ite1 != nums1.end()&&ite2 != nums2.end()) { if(*ite1 == *ite2) { result.push_back(*ite1); ite1++; ite2++; } else if(*ite1 < *ite2) { ite1++; } else { ite2++; } } return result; } };