给你一个未排序的整数数组nums,请你找出其中没有出现的最小的正整数。
请你实现时间复杂度为 O(n) 并且只使用常数级别额外空间的解决方案。
示例1:
输入:nums = [1, 2, 0]
输出:3
输入:nums = [3,4,-1,1]
输出:2
输入:nums = [7,8,9,11,12]
输出:1
思路:哈希表
class Solution {
public:
int firstMissingPositive(vector<int>& nums) {
unordered_map<int, int> hashmap;
for(int i = 0; i < nums.size(); i++)
{
if(nums[i] <= 0) continue;
hashmap[nums[i]]++;
}
int i = 1;
while(true){
if (hashmap.count(i) <= 0) return i;
i++
}
return -1;
}
};