1.简述:
给定一个未排序的整数数组nums,请你找出其中没有出现的最小的正整数
进阶: 空间复杂度 ,时间复杂度
数据范围:
-231<=nums[i]<=231-1
0<=len(nums)<=5*105
输入:
[1,0,2]
返回值:
3
输入:
[-2,3,4,1,5]
返回值:
2
输入:
[4,5,6,8,9]
返回值:
1
2.代码实现:
import java.util.*;
public class Solution {
public int minNumberDisappeared (int[] nums) {
int n = nums.length;
HashMap<Integer, Integer> mp = new HashMap<Integer, Integer>();
//哈希表记录数组中出现的每个数字
for(int i = 0; i < n; i++)
mp.put(nums[i], 1);
int res = 1;
//从1开始找到哈希表中第一个没有出现的正整数
while(mp.containsKey(res))
res++;
return res;
}
}