【发布时间】:2016-03-29 13:59:45
【问题描述】:
我需要使用递归来获得小于数组中第一个整数的数字的计数。我得到一个函数定义为
public static int countGreaterThanFirst(int[]
numbers, int startIndex, int endIndex, int firstNumber){}
我不应该使用循环或全局/静态变量。如何转换下面的实现以满足上述两个条件。我最近问过another 类似的问题,但这有点不同,因为需要跟踪计数变量。如果有人可以提供帮助,我将不胜感激。 下面是我的循环实现。
public static int countGreaterThanFirst(int[] numbers, int startIndex, int endIndex, int firstNumber) {
int greater_than_first = 0;
for (int count = startIndex; count <= endIndex; count++) {
if (numbers[count] > firstNumber) {
greater_than_first++;
}
}
return greater_than_first;
}
【问题讨论】:
-
它有效,但这不是预期的实现。