猿问

从数组中查找第二个最小元素

任何人都可以将其转换为 Java 函数式风格 (lambda):


public int findSecondMin(int arr[]) {


    int min = Integer.MAX_VALUE, secondMin = Integer.MAX_VALUE;

    for (int i = 0; i < arr.length; i++) {

        if (min > arr[i]) {

            secondMin = min;

            min = arr[i];

        } else if (secondMin > arr[i]) {

            secondMin = arr[i];

        }

    }

    return secondMin;

}

我尝试通过应用过滤器来解决这个问题,但它不起作用。


慕码人8056858
浏览 197回答 3
3回答

叮当猫咪

使用IntStream,您可以轻松对其进行排序并跳过第一个元素:public int findSecondMin(int[] arr){&nbsp; &nbsp; return IntStream.of(arr).sorted().skip(1).findFirst().orElse(Integer.MAX_VALUE);}但是,当然,您不必使用流。java.util.Arrays有一个很好的排序方法,然后你可以只取第二个元素:public int findSecondMin(int[] arr){&nbsp; &nbsp; Arrays.sort(arr);&nbsp; &nbsp; return arr.length < 2 ? Integer.MAX_VALUE : arr[1];}为了避免对整个数组进行排序,我们可以采用您的方法并将其调整为流上的自定义缩减:public int findSecondMin(int[] arr){&nbsp; &nbsp; return IntStream.of(arr).boxed().reduce(&nbsp; &nbsp; &nbsp; &nbsp; new int[] {Integer.MAX_VALUE, Integer.MAX_VALUE},&nbsp; &nbsp; &nbsp; &nbsp; (mins, i) -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return new int[] {Math.min(i, mins[0]), Math.min(Math.max(i, mins[0]), mins[1])};&nbsp; &nbsp; &nbsp; &nbsp; }, (mins1, mins2) -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int[] lesser = mins1[0] < mins2[0] ? mins1 : mins2;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int[] larger = mins1[0] < mins2[0] ? mins2 : mins1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return new int[] {lesser[0], Math.min(lesser[1], larger[0])};&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; )[1];}与基于 for 循环的实现相比,它可能更难阅读,但可以并行工作。

湖上湖

在所有数字都是唯一的情况下,即使没有排序也有一种方法。过滤掉最小值并再次请求另一个导致第二个最低值的值。int firstMin = Arrays.stream(arr).min().getAsInt();int secondMin = Arrays.stream(arr).filter(i -> i != firstMin).min().getAsInt();System.out.println(firstMin);&nbsp; // prints 2System.out.println(secondMin); // prints 3编辑:还有另一种使用TreeSet存储已排序值的实现的方法。删除最低的元素并再次请求第一个元素 - 结果是第二个最低的元素:SortedSet<Integer> sortedSet = Arrays.stream(arr)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.boxed()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.collect(Collectors&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.toCollection(TreeSet::new));sortedSet.remove(sortedSet.first());int secondMin = sortedSet.first();System.out.println(secondMin); // prints 3

当年话下

public int min(){&nbsp; int a[] ={2,5,1,3};&nbsp; int min, secondMin = a[0];&nbsp; for(int i=0;i<a.length;i++){&nbsp; &nbsp; &nbsp; &nbsp; if(min>a[i]){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;secondMin =min;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;min= a[i];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; }&nbsp; return secondMin;}我从上面的代码段得到了第二分钟...试试这个.....
随时随地看视频慕课网APP

相关分类

Java
我要回答