【发布时间】:2015-04-17 19:29:30
【问题描述】:
//正如标题所说,我需要找到 int //array 中最大值的索引,所有这些都需要在一个方法中完成这是我的助手 //method 到目前为止的样子
它只返回数组中的最后一个索引,我可以轻松返回最大值,但我不知道如何返回该值的索引
//这是辅助方法
private int recursiveGetIndexOfLargest( int[] list, int count )
{
int index;
int[] y = list;
int temp = count - 1;
if( count > 0 )
{
index = Math.max( list[list.length - 1], list[temp] );
for(int x = 0; x < y.length; x++)
{
if(y[x] == index)
{
return x;
}
}
return recursiveGetIndexOfLargest(list, temp);
}
else
{
return -1;//empty list
}
}
这是调用助手的方法
public int getIndexOfLargest()
{
return recursiveGetIndexOfLargest(list, count);
}
【问题讨论】:
-
我无法将参数更改为任一方法
-
问题是什么?
-
必须是数组?因为你实际上需要一个完整的数组来找到元素,也就是 O(n)。
-
我想您并不是要返回循环变量,而是要返回 y[x]?
-
不,我需要返回出现最大值的索引,所以如果数组是 {1,5,2,3,0} 我需要返回 1