zoukankan      html  css  js  c++  java
  • leetcode[41]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 {
    public:
        int firstMissingPositive(int A[], int n) {
        if(A==NULL||n<1)return 1;
        int i;
        for (i=0;i<n;i++)
        {
            if (A[i]>0&&A[i]!=i+1&&A[i]<=n&&A[i]!=A[A[i]-1])
            {
                int temp=A[i];
                A[i]=A[A[i]-1];
                A[temp-1]=temp;
                i--;
            }
        }
        for (i=0;i<n;i++)
        {
            if (A[i]!=i+1)    return i+1;
        }
        return i+1;
        }
    };
  • 相关阅读:
    Uva10305(dfs)
    Uva572
    Uva122
    Uva679
    Uva136
    Uva489
    Uva133
    Uva1339
    Uva1588
    《世纪的哭泣》读后感 读书笔记
  • 原文地址:https://www.cnblogs.com/Vae1990Silence/p/4283577.html
Copyright © 2011-2022 走看看