I'm trying to reverse an integer array using a recursive method. I'm really bad with recursion so far, and I was wondering if someone could help me with this problem that I'm having.
我试图用递归方法反转一个整数数组。到目前为止,我对递归很不在行,我想知道是否有人能帮我解决这个问题。
Here is my code so far:
这是我目前的代码:
public static int[] reverseArray(int[] array, int startIndex, int endIndex){
int[] tempArray = array;
if(tempArray[startIndex] == array[endIndex]){
return tempArray;
}
else{
tempArray[startIndex] = array[endIndex];
reverseArray(array, startIndex + 1, endIndex - 1);
return tempArray;
}
}
1 个解决方案
#1
4
Your recursive logic is fine: to reverse an array, we reverse the first and last element and do that again on the array without those elements. That means we need to swap the first and the last element together and call the method again, incrementing the first index and decrementing the last index. In your code, however, you are only changing tempArray[startIndex]
and not tempArray[endIndex]
.
您的递归逻辑很好:为了反转一个数组,我们将第一个和最后一个元素颠倒过来,在没有这些元素的数组中再次执行这个操作。这意味着我们需要将第一个和最后一个元素交换到一起,并再次调用该方法,递增第一个索引,递减最后一个索引。然而,在您的代码中,您只是更改了tempArray[startIndex],而不是tempArray[endIndex]。
The base condition is wrong though: there is nothing to do, not when the first element is equal to the last element, but when the first index is greater or equal than the last index (if it is equal, there is only one element to consider and so the reverse is also the same element).
基本条件是错误的:没有什么,当第一个元素等于最后一个元素,但当第一个指数大于或等于最后一个索引(如果它是平等的,只有一个因素考虑,所以反过来也相同的元素)。
Putting this into code, this turns into:
将其放入代码中,就变成:
private static int[] reverseArray(int[] array, int startIndex, int endIndex) {
if (startIndex >= endIndex) { // base condition, nothing to do when there is one or no element to consider
return array;
}
// swap array[startIndex] and array[endIndex]
int temp = array[startIndex];
array[startIndex] = array[endIndex];
array[endIndex] = temp;
// recurse with the decreasing bounds
return reverseArray(array, startIndex + 1, endIndex - 1);
}
Note that I removed the declaration of the tempArray
: we can consider directly array
. Then, we can add a utility method:
注意,我删除了tempArray的声明:我们可以考虑直接数组。然后,我们可以添加一个实用方法:
public static int[] reverseArray(int[] array){
return reverseArray(array.clone(), 0, array.length - 1);
}
Note that I made this method public and the other one private: the one you will want to call will be this one since it hides the recursive implementation. I added a call to clone()
in there so as not to modify the given array when calculating the reverse.
注意,我将这个方法设置为public,而另一个设置为private:您希望调用的这个方法将是这个方法,因为它隐藏了递归实现。我在其中添加了一个对clone()的调用,以便在计算反向时不修改给定的数组。
#1
4
Your recursive logic is fine: to reverse an array, we reverse the first and last element and do that again on the array without those elements. That means we need to swap the first and the last element together and call the method again, incrementing the first index and decrementing the last index. In your code, however, you are only changing tempArray[startIndex]
and not tempArray[endIndex]
.
您的递归逻辑很好:为了反转一个数组,我们将第一个和最后一个元素颠倒过来,在没有这些元素的数组中再次执行这个操作。这意味着我们需要将第一个和最后一个元素交换到一起,并再次调用该方法,递增第一个索引,递减最后一个索引。然而,在您的代码中,您只是更改了tempArray[startIndex],而不是tempArray[endIndex]。
The base condition is wrong though: there is nothing to do, not when the first element is equal to the last element, but when the first index is greater or equal than the last index (if it is equal, there is only one element to consider and so the reverse is also the same element).
基本条件是错误的:没有什么,当第一个元素等于最后一个元素,但当第一个指数大于或等于最后一个索引(如果它是平等的,只有一个因素考虑,所以反过来也相同的元素)。
Putting this into code, this turns into:
将其放入代码中,就变成:
private static int[] reverseArray(int[] array, int startIndex, int endIndex) {
if (startIndex >= endIndex) { // base condition, nothing to do when there is one or no element to consider
return array;
}
// swap array[startIndex] and array[endIndex]
int temp = array[startIndex];
array[startIndex] = array[endIndex];
array[endIndex] = temp;
// recurse with the decreasing bounds
return reverseArray(array, startIndex + 1, endIndex - 1);
}
Note that I removed the declaration of the tempArray
: we can consider directly array
. Then, we can add a utility method:
注意,我删除了tempArray的声明:我们可以考虑直接数组。然后,我们可以添加一个实用方法:
public static int[] reverseArray(int[] array){
return reverseArray(array.clone(), 0, array.length - 1);
}
Note that I made this method public and the other one private: the one you will want to call will be this one since it hides the recursive implementation. I added a call to clone()
in there so as not to modify the given array when calculating the reverse.
注意,我将这个方法设置为public,而另一个设置为private:您希望调用的这个方法将是这个方法,因为它隐藏了递归实现。我在其中添加了一个对clone()的调用,以便在计算反向时不修改给定的数组。