关于比较数组元素
怎么样能快速找到两个数组中相同元素的下标,不用双重循环时间复杂度太高
#include <stdio.h>
#define MAXN 1001
int main()
{
int map[MAXN], i; //根据需要改map的实现,只是提供一种思路
int arrFirst[MAXN] = {2, 5, 1}, arrSecond[MAXN] = {1, 3, 5, 4};
int arrFirstLen = 3, arrSecondLen = 4;
for (i = 0; i < MAXN; ++i)
{
map[i] = -1;
}
for (i = 0; i < arrFirstLen; ++i)
{
map[arrFirst[i]] = i;
}
for (i = 0; i < arrSecondLen; ++i)
{
if (map[arrSecond[i]] != -1)
{
printf("posArrFirst = %d, posArrSecond = %d, val = %d\n",
map[arrSecond[i]], i, arrSecond[i]);
}
}
return 0;
}