踏踏实实积累,不要浮躁!!!
LeetCode https://leetcode-cn.com/problems/first-missing-positive/
题目描述:
题目分析: 要求时间复杂度为 O(n) 空间复杂度为常数级别
思路: 没想到很标准的解法 下面的解法用到了Map 能通过
class Solution {
public int firstMissingPositive(int[] nums) {
Map<Integer,Integer> map;
map = new HashMap<>();
int max = 0;
for(int num : nums)
{
if(num>max)
max = num;
map.put(num,1);
}
for(int i=1;i<max;i++)
if(map.getOrDefault(i,-1)==-1)
{
return i;
}
return max+1;
}
}