LeetCode地址
算法难度
困难
JS代码
/**
* @param {number[]} nums
* @return {number}
*/
var firstMissingPositive = function(nums) {
let n = 0
let p = 0
let temp = {}
for (let i = 0; i < nums.length; i++) {
if (nums[i] <= 0) continue
if (nums[i] === p + 1) {
n = p = p + 1
while (temp[++n]) {
p = n
}
} else {
temp[nums[i]] = true
}
}
return p + 1
};
console.log(firstMissingPositive([1,2,3,7,8,9,11,12]))