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-08-01 15:05:06 发布