The intersection of two arrays
Topic: Given two arrays, write a function to calculate their intersection. Note: Each element in the output result must be unique. We can ignore the order of output results.
Example 1:
- enter:
nums1 = [1,2,2,1], nums2 = [2,2]
- Output:
[2]
Example 2:
- enter:
nums1 = [4,9,5], nums2 = [9,4,9,8,4]
- Output:
[9,4]
Problem-solving idea: Intersection is the set of elements shared by the two sets. Therefore, we must first ensure the uniqueness of the collection, so we select the std::map
container to import the elements and perform deduplication . Then randomly traverse the elements in one of the collections, each element and the other collection to find this element:
- If it exists, it is an intersection element and insert the
vector
middle that needs to be returned . - If not, that is, to find
map
theend()
location, it indicates the presence inA
the collection ofB
elements in the collection does not contain, and that this element is not processed, ie you do not put the returned collection.
class Solution {
public:
vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
vector<int> ret;
set<int> s1;
for(auto e:nums1){
s1.insert(e);
}
set<int> s2;
for(auto e:nums2){
s2.insert(e);
}
for(auto e:s1){
if(s2.find(e) != s2.end()) //s1和s2都共有此元素
ret.push_back(e);
}
return ret;
}
};
The intersection of two arrays II
Array: Given two arrays, write a function to calculate their intersection. The number of occurrences of each element in the output result should be the same as the number of occurrences of the element in the two arrays. We can ignore the order of output results.
Example 1:
- enter:
nums1 = [1,2,2,1], nums2 = [2,2]
- Output:
[2,2]
Example 2:
- enter:
nums1 = [4,9,5], nums2 = [9,4,9,8,4]
- Output:
[4,9]
class Solution {
public:
vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {
unordered_map<int,int> dict;
vector<int> ret;
for(auto e : nums1){
dict[e]++;
}
for(auto e : nums2){
if(dict.count(e)){
ret.push_back(e);
dict[e]--;
if(dict[e] == 0)
dict.erase(e);
}
}
return ret;
}
};
The intersection of two sorted arrays
Problem description: To find the intersection of two sorted arrays, the time complexity is O(m + n)
Problem-solving idea: Find the intersection of two arrays, use two pointers to point to array A and array B respectively, and move forward to the pointer with the smaller number. If two arrays with the same number are encountered, save them until they are Any array is traversed.
int mixed(int arr1[],int n1,int arr2[],int n2,int* mix )
{
int i = 0;
int j = 0;
int k = 0;
//mix = new int[n1];
while (i < n1 && j < n2)
{
if(arr1[i] == arr2[j])
{
mix[k++] = arr1[i];
//k++;
i++;
j++;
}
else if (arr1[i]>arr2[j])
j++;
else if (arr1[i]<arr2[j])
i++;
}
return k;
For the intersection of multiple sets, see Interval Merge
Topic: Given multiple arrays, write a function to calculate their intersection. **Note: **Each element in the output result must be unique. We can ignore the order of output results.
Problem-solving idea: Intersection is the set of elements shared by the two sets. Therefore, we must first ensure the uniqueness of the collection, so we select the std::set
container to import the elements and perform deduplication . Then traverse the elements in one of the collections at will, find each element in the other collection, recursively merge the two collections each time.
vector<int> solution(vector<vector<int>> input){
vector<int> res;
if (input.size()==0){
return res;
}
if(input.size()==1)
return input[0];
mergeMultiSet(input,1);
return input[0];
}
void mergeMultiSet(vector<vector<int>>&input,int size){
if (size>=input.size()) return;
input[0]=mergeTwoSet(input[0],input[size]);
mergeMultiSet(input,size+1);
}
vector<int> mergeTwoSet(vector<int>inputone,vector<int>inputtwo){
set<int> setone;
for(auto i:inputone)
setone.insert(i);
int length=inputtwo.size();
vector<int> res;
for(int i=0;i<length;i++){
if (setone.find(inputtwo[i])!=setone.end())
res.push_back(inputtwo[i]);
}
return res;
}
The intersection of two linked lists
Topic: Given two non-descending linked list sequences S1 and S2, the design function constructs a new linked list S3 that is the intersection of S1 and S2
typedef int Status;
typedef int ElemType;//假设线性表中的元素均为整型
typedef struct LNode
{
ElemType data;
struct LNode *next;
} LNode,*LinkList;
void AddList(LinkList &A,LinkList &B,LinkList &C)
{
LNode *pa,*pb,*pc;
pa=A->next;
pb=B->next;
pc=C;
while(pa&&pb)
{
if((pa->data)==(pb->data))
{
pc->next=pa;
pa=pa->next;
pb=pb->next;
pc=pc->next;
}
else if((pa->data)<(pb->data))
{
pa=pa->next;
}
else
pb=pb->next;
}
}