public int removeMin(Integer[] arr, int count) {
Integer[] tempArr = new Integer[arr.length -1];
int index = 0;
for (int i = 1; i<arr.length; i++) {
tempArr[index] = arr[i] - arr[0];
index++;
}
count = count+1;
if (tempArr.length == 0){
return count;
}else{
removeMin(tempArr, count);
}
return count;
}
我希望函数在
tempArr.length == 0
时返回计数为 4。
输入参数:
Integer[] arr = {2,5,8,11};
int count = 0;
我期望它返回 4,但它返回 1。
您需要按如下方式更改您的条件和回报。
if (tempArr.length != 0){
return removeMin(tempArr, count);
}
return count;
这是完整的代码。
public static int removeMin(int[] arr, int count) {
int[] tempArr = new int[arr.length -1];
int index = 0;
for (int i = 1; i<arr.length; i++) {
tempArr[index] = arr[i] - arr[0];
index++;
}
System.out.println(Arrays.toString(arr));
count = count+1;
if (tempArr.length != 0){
return removeMin(tempArr, count);
}
return count;
}