41. First Missing Positive
Given an unsorted integer array, find the smallest missing positive integer.
Example 1:
Input: [1,2,0] Output: 3
Example 2:
Input: [3,4,-1,1] Output: 2
Example 3:
Input: [7,8,9,11,12] Output: 1题意:给定一个无序数组,保持数组不重复找出最小的正整数代码如下:
/** * @param {number[]} nums * @return {number} */ var firstMissingPositive = function(nums) { let arr=[]; arr[0]=1 for(var i=0;i<nums.length;i++){ if(nums[i]>=0) arr[nums[i]]=1; } for(var i=0;i<arr.length;i++){ if(arr[i]!==1) return i; } return arr.length; };
原文地址:https://www.cnblogs.com/xingguozhiming/p/10424845.html
时间: 2024-11-07 06:29:33