public class Solution {
/**
* @param A: an array of integers
* @return: an integer
*/
public int firstMissingPositive(int[] A) {
// write your code here
if (A==null||A.length<=0){
return 1;
}
if (A.length==1){
if (A[0]==1){
return 2;
}
else {
return 1;
}
}
Arrays.sort(A);
if (A[0]>1){
return 1;
}
int length = A.length-1;
for (int i=0;i<length;i++){
if (A[i]>=0){
if (A[i+1]-A[i]>1){
return A[i]+1;
}
}
}
return A[length]+1;
}
}
丟失的第一個正整數_LintCode
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.