【问题标题】:in a recursive method, find index of largest value in an array在递归方法中,找到数组中最大值的索引
【发布时间】: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

标签: java arrays recursion


【解决方案1】:

试试这个:

 int recursiveGetIndexOfLargest(int[] list, int count)
 {
   if (count == list.length - 1) return count;

   int index = recursiveGetIndexOfLargest(list, count + 1);
   return list[count] > list[index] ? count : index;
 }

 int[] arr = {1, 5, 2, 3, 0};
 System.out.println(recursiveGetIndexOfLargest(arr, 0));

【讨论】:

    【解决方案2】:
    int findLargestIndex(int[] array, int currentPos, int currentLargestIndex)
    {
      if(currentPos == array.length) 
          return currentLargestIndex;
      if(array[currentPost] > array[currentLargestIndex]
          return findLargestIndex(array,currentPos+1, currentPos);
      else
          return findLargestIndex(array,currentPos+1, currentLargestIndex);
    }
    

    这实际上是一个 O(n) for 循环完成的递归。 你可以这样开始:

    int result = findLargestIndex(array,0,0);
    

    这可行,但会改变数组。

    void findLargestIndex(int[] array, int currentPos)
    {
         if(currentPos == array.size()) return;
         array[0] = (array[currentPos] < array[currenPos + 1] ? currentPos + 1 : currentPos;
         findLargestIndex(int[] array, currentPos + 1);
    }
    

    最大的索引将存储在数组[0](这会改变数组)。

    你只需启动函数:

     findLargestIndex(array,0);
    

    【讨论】:

    • 我无法更改它必须保留的方法的参数(int[] list, int count)
    • 好的,请稍等。您可以使用字段来存储 currentLargestIndex 还是算法问题解决问题?
    • 大声笑我不能改变数组,因为在那个方法之后我需要调用另一个递归地反转数组顺序的方法
    • 那我试试移动窗口的方法。
    • 我认为不改变数组是不可能的,因为我必须交换两个元素。这是作业吗?我可以获得完整的问题陈述吗?
    【解决方案3】:

    谢谢你!!!!!!!!!!

    参数计数实际上是数组的大小,所以我把它改了一点

    private int recursiveGetIndexOfLargest( int[] list, int count )
    {
        int index;
        int temp = count - 1;
        if( temp == 0 )
        {
            return temp;
        }
    
        else
        {
            index = recursiveGetIndexOfLargest(list, temp);
            return list[temp] > list[index] ? temp : index;
        }
    }
    

    现在它可以工作了,该死的我浪费了几个小时失败

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-31
      • 1970-01-01
      • 2016-12-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多