思路2:索引法
以空间换时间,把集合(感谢网友的指正,集合里面的元素是不重复的!)中的元素作为数组下表的索引。来看例子:
A= {1 ,12, 13, 25},那Asub[1] = 3,Asub[12] = 1 ,Asub[13] = 1 ,Asub[25] = 1 ;
B={1, 2, 3, 15 ,}那Bsub[1] = 1; Bsub[2] = 1; Bsub[3] = 1; Bsub[15] = 1;
对元素少的集合扫一遍,发现Asub[1] = 3 和Bsub[1] = 1有相同的索引1,并且重复度为1,所以交集肯定包括{1, 1}; Bsub[2] = 1而Asub[2] = 0,表示无交集,依次类推,可以得到集合A和B的交集。
假设集合中存在负数,可以把集合分成正整数和负整数(加个负号变正整数)两部分,解法同上!
优点:速度快,时间复杂度O(N)
缺点:空间消耗大,以空间换取时间
这是我看到题目第一个想到的算法,再来想到排序法,而集合压缩是有感而发的,索引法的缺点是空间消耗多,原因是可能索引值太大,要申请很多的不必要的空间,这个缺点也是有克服的方法的,就是采用哈希查找,找到一个比较合适的哈希函数,把索引的值减小了,从而减少消耗的内存空间。比如哈希函数为f(x) = (x + MOD) % MOD (除留余数法,MOD为常数),还有平方取中法、折叠法等方法,然而,无论哈希函数设计有多么精细,都会产生冲突现象,也就是2个关键字处理函数的结果映射在了同一位置上,因此,有一些方法可以避免冲突。这里没有仔细钻研,只提供一些思路,有兴趣的朋友可以继续研究。
code:(我的代码仅适用与正整数部分,未处理负数)
/*
Tencent: A、B两个整数集合,设计一个算法求他们的交集,尽可能的高效
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define M 6
#define N 5
int Mymin(int a, int b)
{
return a < b ? a : b;
}
int main(void)
{
int A[] = {1, 10, 12, 23, 5, 45};
int B[] = {1, 10, 12, 123, 52};
//find MaxNumber in A
int ifindA = 0;
int MaxInA = A[0];
for(ifindA = 0; ifindA < M; ifindA++)
{
MaxInA = MaxInA > A[ifindA] ? MaxInA : A[ifindA];
}
//find MaxNumber in B
int ifindB = 0;
int MaxInB = 0;
for(ifindB = 0; ifindB < M; ifindB++)
{
MaxInB = MaxInB > A[ifindB] ? MaxInB : A[ifindB];
}
int *AsubPositive = (int *)malloc(sizeof(int) * (MaxInA + 1));
int *BsubPositive = (int *)malloc(sizeof(int) * (MaxInB + 1));
memset(AsubPositive, 0, sizeof(int) * (MaxInA + 1));
memset(BsubPositive, 0, sizeof(int) * (MaxInB + 1));
//COPY Positive and Negative numbers of A
int i = 0;
for(i = 0; i < M; i++)
{
AsubPositive[A[i]]++;
}
//COPY Positive and Negative numbers of B
int j = 0;
for(j = 0; j < N; j++)
{
BsubPositive[B[j]]++;
}
int k = 0;
int icount = 0;
//扫描AsubNegative和BsubPositive
printf("the Intersection of A and B is : { ");
for(k = 0; k < M; k++)
{
//有交集输出该数
icount = Mymin(AsubPositive[A[k]], BsubPositive[A[k]]);
if(icount == 1)
{
printf("%-3d",A[k]);
}
A[k] = 0;
}
printf(" }");
return 0;
}
