分析
数字范围是int;数字数量为10^5;求未给出的最小正整数
声明数组空间:10^5+5
情况1:数字大小全部在10^5内,直接遍历一遍即可
撑死大小也就是10^5+1
情况2:数字大小有在10^5 2侧,排除掉2侧的进行遍历
此时10^5内必然有更多间隙,因此最小一定在10^5内
代码
#include<bits/stdc++.h>
using namespace std;
int quantity;
vector<int>hash_table(100005);
int main() {
fill(hash_table.begin(), hash_table.end(), -1);
scanf("%d", &quantity);
int x;
for (int i = 0; i < quantity; i++) {
scanf("%d", &x);
if (x > 0 && x < 100005)
hash_table[x] = x;
}
for (int i = 1; i < hash_table.size(); i++) {
if (hash_table[i] == -1) {
printf("%d", i);
break;
}
}
return 0;
}
本文介绍了一种高效算法,用于从大量整数中找出未出现的最小正整数。通过使用哈希表,算法能在O(n)时间内完成任务,适用于数字范围在10^5内的场景。
1120

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



