给定一个无序数组arr,找到数组中未出现的最小正整数
例如arr = [-1, 2, 3, 4]。返回1
arr = [1, 2, 3, 4]。返回5
[要求]
时间复杂度为O(n)O(n),空间复杂度为O(1)O(1)
public static void main(String args[]) {
System.out.println(minNumberdisappered(new int[]{-1,0,2,3,1}));
}
public static int minNumberdisappered(int[] arr) {
int current=0;
int size=arr.length;
int finalResult=-1;
Arrays.sort(arr);
for(int i=0;i<size;i++){
current=arr[i];
if(current<=0){
continue;
}
if(i>=1){
if(arr[i-1]<=0){
if(current>=2){
finalResult=1;
}
}
}
if(i!=(size-1)){
if(arr[i+1]-current>=2){
finalResult = current+1;
}
}
else{
if(arr[size-1]-arr[size-2]>=2){
finalResult = current+1;
}
}
}
if(finalResult<0){
return current+1;
}else{
return finalResult;
}
}