【发布时间】:2016-03-29 13:06:08
【问题描述】:
我有一个问题想要解决。我有一个整数数组,我想从中获取最小整数。我还得到了一个应该遵守的函数定义。我不应该在我的解决方案中使用循环或静态变量。我该如何解决这个问题?这是我的功能。
public static int findMin(int[] numbers, int startIndex, int endIndex){}
请帮忙。我已经用循环尝试了这个实现。
public static int findMin(int[] numbers, int startIndex, int endIndex) {
int min = 0;
for (int count = startIndex; count <= endIndex; count++) {
if (numbers[count] < min) {
min = numbers[count];
}
}
return min;
}
【问题讨论】:
-
我将把使用流的 Java 8 解决方案放在这里。以防万一:
Arrays.asList(numbers).stream().min(Integer::compareTo).get();