給定兩個數組,編寫一個函數來計算它們的交集。
示例 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( ) ,merge( )
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
|
merge
template <class InputIterator1, class InputIterator2, class OutputIterator> OutputIterator merge (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); *result++ = (*first2<*first1)? *first2++ : *first1++; } }
// merge algorithm example #include <iostream> // std::cout #include <algorithm> // std::merge, 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); std::sort (first,first+5); std::sort (second,second+5); std::merge (first,first+5,second,second+5,v.begin()); std::cout << "The resulting vector contains:"; for (std::vector<int>::iterator it=v.begin(); it!=v.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; return 0; }
Output:
The resulting vector contains: 5 10 10 15 20 20 25 30 40 50 |