By Jalan
知识工具需求
数学
数据结构和算法
- hash散列
语言
- cpp动态申请硬干大数组.(大数组常伴吾身).
题干
压注在[1,10^4]第一个压到没别人压的数字的人获胜.
输入条件
给一个N<=10^5是参与人数,后面跟着N个数字.
输出条件
输出赢了的人的编号.
没有输出None
题解
第一次
思路
hash散列10^4储存.从前向后遍历,第一个在散列里是1的数获胜.
预期时间复杂度
n
编写用时
10分钟
代码
CPP
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char const *argv[])
{
short *hashList=(short*)malloc(sizeof(int)*10001);
for (int i = 0; i < 10001; i++)
{
hashList[i]=0;
}
int N;
scanf("%d",&N);
short *record=new short[100000]{0};
for (int i = 0; i < N; i++)
{
scanf("%hd",&record[i]);
hashList[record[i]]++;
}
for (int i = 0; i < N; i++)
{
if (hashList[record[i]]==1)
{
printf("%d",record[i]);
return 0;
}
}
printf("None");
return 0;
}
运行用时
结尾
看在我写了这么多注释的份上可以给我点个赞嘛,求求惹=]砰砰砰,给我加点写下去的油呀@.@