350. 兩個陣列的交集 II
給定兩個陣列,編寫一個函式來計算它們的交集。
示例 1:
輸入: nums1 = [1,2,2,1], nums2 = [2,2]
輸出: [2,2]
示例 2:
輸入: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
輸出: [4,9]
說明:
輸出結果中每個元素出現的次數,應與元素在兩個陣列中出現的次數一致。
我們可以不考慮輸出結果的順序。
進階:
如果給定的陣列已經排好序呢?你將如何優化你的演算法?
如果nums1的大小比nums2小很多,哪種方法更優?
如果nums2的元素儲存在磁碟上,磁碟記憶體是有限的,並且你不能一次載入所有的元素到記憶體中,你該怎麼辦?
連結:https://leetcode-cn.com/problems/intersection-of-two-arrays-ii
class Solution { public: vector<int> intersect(vector<int>& nums1, vector<int>& nums2) { sort(nums1.begin(),nums1.end()); sort(nums2.begin(),nums2.end()); vector<int>res; set_intersection(nums1.begin(),nums1.end(),nums2.begin(),nums2.end(),back_inserter(res));return res; } };
C++ STL 提供求交集的函式 set_intersection( ) 、求集合差的函式set_difference( ) 和合並兩個集合的函式 set_union( )
set_intersection( )
nums1:1,2,2,1
nums2:2,2
#include <algorithm> // set_intersection #include <iterator> // insert_iterator vector<int> intersection(vector<int>& nums1, vector<int>& nums2) { vector<int> res; set<int> cod1(nums1.begin(),nums1.end()); set<int> cod2(nums2.begin(),nums2.end()); std::set_intersection(cod1.begin(),cod1.end(),cod2.begin(),cod2.end(),insert_iterator<vector<int>>(res,res.begin())); return res; // res:2 }
vector<int> intersect(vector<int>& nums1, vector<int>& nums2) { vector<int> res; std::sort(nums1.begin(),nums1.end()); std::sort(nums2.begin(),nums2.end()); std::set_intersection(nums1.begin(),nums1.end(),nums2.begin(),nums2.end(),insert_iterator<vector<int>>(res,res.begin())); return res; // res:2,2 }
首先傳遞的容器必須是排序的,set 容器中元素預設是排序的,而 vector 需要呼叫 sort 函式進行排序。其次set_intersection( )中最後存放交集的容器的容量必須要足夠大到能放下所有的元素,即函式只執行復制,不是插入!但是模板 insert_iterator 可以將複製轉換為插入,可以解決該問題。
set_intersection( ) 不是 set 的方法,而是一個通用函式,而 set 函式必須要滿足這些演算法!使用 set 時,可以自動忽略重複的元素,而使用 vector 時可以保留重複的元素,即保留了‘個數’這一資訊。
該函式的時間複雜度為線性複雜度,其實現為:
template <class InputIterator1, class InputIterator2, class OutputIterator> OutputIterator set_intersection (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result) { while (first1!=last1 && first2!=last2) { if (*first1<*first2) ++first1; else if (*first2<*first1) ++first2; else { *result = *first1; ++result; ++first1; ++first2; } } return result; }
// set_intersection example #include <iostream> // std::cout #include <algorithm> // std::set_intersection, std::sort #include <vector> // std::vector int main () { int first[] = {5,10,15,20,25}; int second[] = {50,40,30,20,10}; std::vector<int> v(10); // 0 0 0 0 0 0 0 0 0 0 std::vector<int>::iterator it; std::sort (first,first+5); // 5 10 15 20 25 std::sort (second,second+5); // 10 20 30 40 50 it=std::set_intersection (first, first+5, second, second+5, v.begin()); // 10 20 0 0 0 0 0 0 0 0 v.resize(it-v.begin()); // 10 20 std::cout << "The intersection has " << (v.size()) << " elements:\n"; for (it=v.begin(); it!=v.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; return 0; }
Output
The intersection has 2 elements: 10 20 |
set_difference( )
template <class InputIterator1, class InputIterator2, class OutputIterator> OutputIterator set_difference (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result) { while (first1!=last1 && first2!=last2) { if (*first1<*first2) { *result = *first1; ++result; ++first1; } else if (*first2<*first1) ++first2; else { ++first1; ++first2; } } return std::copy(first1,last1,result); }
// set_difference example #include <iostream> // std::cout #include <algorithm> // std::set_difference, std::sort #include <vector> // std::vector int main () { int first[] = {5,10,15,20,25}; int second[] = {50,40,30,20,10}; std::vector<int> v(10); // 0 0 0 0 0 0 0 0 0 0 std::vector<int>::iterator it; std::sort (first,first+5); // 5 10 15 20 25 std::sort (second,second+5); // 10 20 30 40 50 it=std::set_difference (first, first+5, second, second+5, v.begin()); // 5 15 25 0 0 0 0 0 0 0 v.resize(it-v.begin()); // 5 15 25 std::cout << "The difference has " << (v.size()) << " elements:\n"; for (it=v.begin(); it!=v.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; return 0; }
Output:
The difference has 3 elements: 5 15 25 |
set_union( )
template <class InputIterator1, class InputIterator2, class OutputIterator> OutputIterator set_union (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result) { while (true) { if (first1==last1) return std::copy(first2,last2,result); if (first2==last2) return std::copy(first1,last1,result); if (*first1<*first2) { *result = *first1; ++first1; } else if (*first2<*first1) { *result = *first2; ++first2; } else { *result = *first1; ++first1; ++first2; } ++result; } }
// set_union example #include <iostream> // std::cout #include <algorithm> // std::set_union, std::sort #include <vector> // std::vector int main () { int first[] = {5,10,15,20,25}; int second[] = {50,40,30,20,10}; std::vector<int> v(10); // 0 0 0 0 0 0 0 0 0 0 std::vector<int>::iterator it; std::sort (first,first+5); // 5 10 15 20 25 std::sort (second,second+5); // 10 20 30 40 50 it=std::set_union (first, first+5, second, second+5, v.begin()); // 5 10 15 20 25 30 40 50 0 0 v.resize(it-v.begin()); // 5 10 15 20 25 30 40 50 std::cout << "The union has " << (v.size()) << " elements:\n"; for (it=v.begin(); it!=v.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; return 0; }
Output:
The union has 8 elements: 5 10 15 20 25 30 40 50 |