41. First Missing Positive(js)

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;

};

 

posted @ 2019-02-23 23:19  mingL  阅读(98)  评论(0编辑  收藏  举报