缺失的第一个正整数
2023/7/3...小于 1 分钟
缺失的第一个正整数
描述
给定一个未排序的整数数组nums,请你找出其中没有出现的最小的正整数。中间没出现也是
实现思路
陷入误区:下意识先先想到了排序
解题代码
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;
}