求两个数组的交集

题目大意

给定两个数组,写一个函数来计算它们的交集。
每个在结果中的元素必定是唯一的。
我们可以不考虑输出结果的顺序。
数组不一定是有序的。

解法

1.暴力破解,两重for循环。

时间复杂度 O(n²)。

2.利用计数排序的思想

利用数组C记录数组A中每个元素出现的次数,再统计数组B每一项出现的次数,做好标记,输出交集即可。具体实现时,这里的数组都是静态的。

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <time.h>
#include <algorithm>
#include <iostream>
#include <queue>
#include <stack>
#include <vector>
#include <string>
using namespace std;
#pragma warning(disable:4996)   //方便做题,我把常用的都写上了。

int main() {
	int a[5] = { 1,2,3,4,6 };
	int b[5] = { 5,5,1,1,2 };
	int c[10] = { 0 };
	int d[10] = { 0 };
	for (int i = 0; i < 5; i++) {
		c[a[i]]++;
	}
	for (int i = 0; i < 5; i++) {
		if (c[b[i]] != 0) { d[b[i]] = 1; }
	}
	for (int i = 0; i < 10; i++) {
		if (d[i] != 0) { cout << i << " "; }
	}
	return 0;
}

输出正确,这个方法的时间复杂度只有O(n)。

3.先对两个数组分别排序,然后再计算交集

排序的时间复杂度最小可以是O(nlogn),计算交集时间复杂度最多是O(m+n),m和n分别是两个数组的长度。整体时间复杂度最少O(nlogn)。

利用hash表思想

先将数组A的元素一对一map,键与值都是数组元素值。然后将数组B的元素与map的值比较,交集放入另一个map,可以避免重复元素。最后输出第二个map即可。

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <time.h>
#include <algorithm>
#include <iostream>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <map>
#include <iterator>
using namespace std;
#pragma warning(disable:4996)

int main() {
	map<int, int>mapint;    //记录第一个数组A的键值对
	map<int, int>mapint2;    //记录第二个数组B的键值对
	int a[5] = { 1,2,3,4,6 };
	int b[5] = { 5,5,1,1,2 };
	for (int i = 0; i < 5; i++) {
		mapint[a[i]] = a[i];   //map自动会省略重复元素
	}
	// find 返回迭代器指向当前查找元素的位置否则返回map::end()位置
	map<int, int>::iterator iter;   //迭代器用于查找。
	for (int i = 0; i < 5; i++) {
		iter = mapint.find(b[i]);
		if (iter != mapint.end())
			mapint2[iter->second]=iter->second;   //如果找到放到第二个map里面去。
	}
	map<int,int>::iterator it;//声明一个迭代器,来访问vector容器。
	for (it = mapint2.begin(); it != mapint2.end(); it++)
	{
		cout << it->second << " ";
	}
	return 0;
}

for循环的时间复杂度是O(n),但是find函数的搜索复杂度大概是O(logn),那么总的时间复杂度大概就是O(nlogn)了。

这是关于求数组交集的四种方法。

发布了27 篇原创文章 · 获赞 0 · 访问量 414

猜你喜欢

转载自blog.csdn.net/weixin_40007143/article/details/104088390