找出只出现过一次的数,用各种排序必然超时,需要用数组做hash表
Sample Input 1:
7 5 31 5 88 67 88 17
Sample Output 1:
31
Sample Input 2:
5 888 666 666 888 888
Sample Output 2:
None
#include <stdio.h>
#include <stdlib.h>
#define MAX 10000
#define MAX1 100000
int s[MAX] = {0},t[MAX1];
int main()
{
long int i;
long int N;
scanf("%ld", &N);
for (i = 0; i < N; i++)
{
scanf("%d",&t[i]);
s[t[i]]++;
}
for (i = 0; i < N; i++)
{
if(s[t[i]] == 1)
break;
}
if (i == N)
{
printf("None");
}
else
{
printf("%d", t[i]);
}
return 0;
}
本文提供了一种使用哈希表实现的方法来找出数组中只出现一次的数。通过初始化一个大小为MAX的数组作为哈希表,并遍历输入的整数数组进行计数,最终找到计数为1的元素即为所求。
246

被折叠的 条评论
为什么被折叠?



