笔试题:求两个乱序数组的共公数组

#include 
#include 
#include 
using namespace std;
vector find(int a[],int n,int b[],int m)
{
	int i = 0;
	vector result;
	bitset<100> bt1;
	bitset<100> bt2;
	//适合分布集中的大型数据较小的数组。
	for (; i < n; i++)
	{
		bt1.set(a[i]);
	}
	for (i = 0; i < m; i++)
	{
		bt2.set(b[i]);
	}
	for (i = 0; i < 100; i++)
	{
		if (bt1.test(i) == 1 && bt2.test(i) == 1)
		{
			result.push_back(i);
		}
	}
	return result;
}
int main()
{
	int a[] = {3,4,5,6,7,8,1,3,4,2,4,5,14};
	int b[] = { 1, 2, 3, 4, 1, 5, 6, 6, 45, 5, 5, 3, 14, 2 };
	vector v = find(a, sizeof(a) / sizeof(int), b, sizeof(b) / sizeof(int));
	for (int i = 0; i < v.size(); ++i)
	{
		cout << v[i] << "  ";
	}
	cout << endl;
	return 0;
}

你可能感兴趣的:(笔试题)