public class Test {
public static int solution(int[] A) {
int temp;
for(int i=0;i<A.length-1;i++) {
for (int j = 0; j < A.length - i - 1; j++) {
if (A[j + 1] < A[j]) {
temp = A[j];
A[j] = A[j + 1];
A[j + 1] = temp;
}
}
}
int res=1;
if (A[0]>1||A[A.length-1]<1){ res=1;}
else{
for(int k=0;k<A.length-1;k++) {
if(A[k]<A[k+1]-1){res=A[k]+1; break;}
else{
res=A[A.length-1]+1;
}
}
}
if (res<1 ) res=1;
return res;
}
public static void main(String[] args) {
int[]A = {1,2,3};
int[]A1 = {1, 3, 6, 4, 1, 2};
int[]A2 = {-1, -3};
int[]A4 = {-1, 1,2};
System.out.println (solution(A));
System.out.println (solution(A1));
System.out.println (solution(A2));
System.out.println (solution(A4));
}
}
给出一个数组,要求返回不在数组中出现过的最小正整数
最新推荐文章于 2024-11-19 12:01:10 发布
该博客展示了一段Java代码,用于对整数数组进行冒泡排序,并找到数组中连续元素间的间隔。主要涉及数组操作、比较和交换元素的逻辑,以及判断序列条件的实现。
1035

被折叠的 条评论
为什么被折叠?



