LintCode-丢失的第一个正整数

题目描述:

  给出一个无序的正数数组,找出其中没有出现的最小正整数。

样例

  如果给出 [1,2,0], return 3
  如果给出 [3,4,-1,1], return 2

挑战

  只允许时间复杂度O(n)的算法,并且只能使用常数级别的空间。

 1 public class Solution {
 2     /**    
 3      * @param A: an array of integers
 4      * @return: an integer
 5      */
 6     public int firstMissingPositive(int[] A) {
 7        int fmp = 1;
 8          for(int i=0;i<A.length;i++){
 9              if(A[i]==fmp){
10                  A[i]=-1;
11                  fmp++;
12                  i=-1;
13              }
14          }
15          return fmp;
16     }
17 }
原文地址:https://www.cnblogs.com/xiaocainiao2hao/p/5364681.html