[LeetCode][Java] First Missing Positive

题目:

Given an unsorted integer array, find the first missing positive integer.

For example,

Given [1,2,0] return 3,

and [3,4,-1,1] return 2.

Your algorithm should run in O(n) time and uses constant space.

题意:

给定一个未排序的整数数组,找出第一个错过的正整数。

比如:

给定[1,2,0] 返回3,

[3,4,-1,1]返回2.

算法要求O(n)
时间复杂度和常数空间。

算法分析:

最简单的思路就是对数组进行快速排序,但是由于要求O(n)
的时间复杂度,所以快速排序显然是行不通的。

因此为了搜寻元素,这里采用数组的下标作为其索引,即对数组的元素进行交换,将正数i放到i-1的位置上,对于负数和大于数组长度的元素弃之不顾。这样线性扫描一下数组就能得到第一个不存在的正数,即第j位置的元素不等于j+1

AC代码:

public class Solution
{
    public int firstMissingPositive(int[] A)
    {
        //将正数放到值-1的位置上,这样1放在0号位置,2放在1号位置,。。。。
        if(A==null || A.length==0)
            return 1;
        for(int i=0;i<A.length;i++)
        {
            if(A[i]<=A.length && A[i]>0 && A[A[i]-1]!=A[i])//这里A[A[i]-1]!=A[i]这个限制条件的意思是,已经满足条件的就不交换了
            {
                int temp=A[A[i]-1];
                A[A[i]-1]=A[i];
                A[i]=temp;
                i--;
            }
        }

        for(int i=0;i<A.length;i++)
        {
            if(A[i]!=(i+1))
                return i+1;
        }
        return A.length+1;
    }
}

版权声明:本文为博主原创文章,转载注明出处

时间: 2024-08-29 09:48:42

[LeetCode][Java] First Missing Positive的相关文章

【leetcode】 First Missing Positive

[LeetCode]First Missing Positive Given an unsorted integer array, find the first missing positive integer. For example, Given [1,2,0] return 3, and [3,4,-1,1] return 2. Your algorithm should run in O(n) time and uses constant space. 找到第一个没有出现的正整数 思路:

[array] leetcode - 41. First Missing Positive - Hard

leetcode - 41. First Missing Positive - Hard descrition Given an unsorted integer array, find the first missing positive integer. For example, Given [1,2,0] return 3, and [3,4,-1,1] return 2. Your algorithm should run in O(n) time and uses constant s

Java for LeetCode 041 First Missing Positive

Given an unsorted integer array, find the first missing positive integer. For example, Given [1,2,0] return 3, and [3,4,-1,1] return 2. Your algorithm should run in O(n) time and uses constant space. 解题思路一: 刚看到题目的时候感觉无从下手,后来仔细理解题意,需要找到first missing p

【LeetCode】First Missing Positive 解题报告

[题目] Given an unsorted integer array, find the first missing positive integer. For example, Given [1,2,0] return 3, and [3,4,-1,1] return 2. Your algorithm should run in O(n) time and uses constant space. [解析] 题意:给定一个数组,找出第一个缺失的正数,要求时间复杂度为O(n),空间复杂度为

[LeetCode][JavaScript]First Missing Positive

First Missing Positive Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] return 3,and [3,4,-1,1] return 2. Your algorithm should run in O(n) time and uses constant space. https://leetcode.com/problems

LeetCode题解-----First Missing Positive

Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] return 3,and [3,4,-1,1] return 2. Your algorithm should run in O(n) time and uses constant space. 分析: 因为数组的大小为n,因此那个缺失的整数只可能的范围[1,n+1] 方法一:需要O(n)的空间,设

LeetCode - 41. First Missing Positive

41. First Missing Positive Problem's Link ---------------------------------------------------------------------------- Mean: 给你一组整数,找出第一个空缺的正整数. 要求:时间O(n),空间O(n). analyse: 这题时间O(n)想了半天没想到,用O(n*logn)过的. 然后看了discuss,想法非常巧妙,自愧不如. Time complexity: O(N) v

LeetCode 041 First Missing Positive

题目要求:First Missing Positive Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] return 3,and [3,4,-1,1] return 2. Your algorithm should run in O(n) time and uses constant space. 代码如下: class Solution { p

leetCode 41.First Missing Positive (第一个丢失的正数) 解题思路和方法

First Missing Positive Given an unsorted integer array, find the first missing positive integer. For example, Given [1,2,0] return 3, and [3,4,-1,1] return 2. Your algorithm should run in O(n) time and uses constant space. 思路:这个题刚開始是没有思路的,难就难在O(n)时间内