數組查找最大值這是我的測試陣列使用遞歸
int [] A = {1,2,7,3,5,6};
這是方法
public static int largest(int [] A)
{
int temp = A[0];
return largestRec(A, 0, A.length - 1, temp);
}
// WRITE THIS METHOD that returns the largest of the elements in A
// that are indexed from low to high. RECURSIVELY!
private static int largestRec(int [] A, int low, int high, int temp)
{
if (low == high)
return A[low];
if (low <= A.length){
if (A[low] > temp){
temp = A[low];
}
largestRec(A, low+1, high, temp);
}
return temp
}
爲什麼TEM復位並返回A[0]
這是1
?
感謝這解決了一切。 – user3247435