Skip to content

Commit cc57318

Browse files
authored
Update intersection-of-two-arrays.cpp
1 parent de34320 commit cc57318

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

C++/intersection-of-two-arrays.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,16 @@ class Solution3 {
7777
return result;
7878
}
7979
};
80+
81+
// Time: O(mlogm + nlogn)
82+
// Space: O(m + n)
83+
class Solution4 {
84+
public:
85+
vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
86+
vector<int> result;
87+
sort(begin(nums1), end(nums1)); sort(begin(nums2), end(nums2));
88+
set_intersection(cbegin(nums1), cend(nums1), cbegin(nums2), cend(nums2), back_inserter(result));
89+
result.erase(unique(begin(result), end(result)), end(result));
90+
return result;
91+
}
92+
};

0 commit comments

Comments
 (0)