package SumStudy; import java.util.ArrayList; import java.util.List; public class MinInt { public static void main(String[] args) { // int[] arr = new int[]{8,8 ,4,138,1, 2,85, -1, 1, 18, 5, 9, 12, -5}; int[] arr = new int[]{2,3,4,5,6}; int minInt = getMinInt(arr); System.out.println("最小隐藏正数:"+minInt); } /** * 获得最小正数 * * @param arr * @return */ public static int getMinInt(int[] arr) { List<Integer> newArr = new ArrayList<>(); for (int i : arr) { if (i <= 0) { continue; } if (newArr.size() < 1) { newArr.add(i); continue; } int size = newArr.size(); int j = i; for (int k = 0; k < size; k++) { if (newArr.contains(j)) { break; } int arr_val = newArr.get(k); if(arr_val>j){ newArr.set(k, j); j = arr_val; } if (size - 1 == k) { newArr.add(j); } } } System.out.println("新数组"+newArr); int min =0; for (int l = 0; l <newArr.size();l++) { if(l+1 !=newArr.get(l)){ if(l==0){ min=1; }else { min = newArr.get(l-1)+1; } break; } } if(min==0){ min = newArr.size()+1; } return min; } }
寻找最小未出现的正整数
最新推荐文章于 2023-08-25 15:41:35 发布