【问题标题】:Getting the largest element in an array using recursion使用递归获取数组中的最大元素
【发布时间】:2011-12-29 14:08:43
【问题描述】:

我有一个任务是使用递归来获取任何给定数组中的最大元素。我有以下代码,除非最大的元素是数组中的最后一个,否则它将起作用。

不确定如何纠正?

import java.util.Scanner;
public class RecursionLargestInArray
{
public static void main (String[] args)
{
    int max = -999;
    Scanner scan = new Scanner (System.in);
    System.out.print("Enter the size of the array: ");
    int arraySize = scan.nextInt();
    int[] myArray = new int[arraySize];
    System.out.print("Enter the " + arraySize + " values of the array: ");
    for (int i = 0; i < myArray.length; i++)
        myArray[i] = scan.nextInt();
    for (int i = 0; i < myArray.length; i++)
        System.out.println(myArray[i]);
    System.out.println("In the array entered, the larget value is "
                        + getLargest(myArray, max) + ".");
}

public static int getLargest(int[] myArray, int max)
{    
    int i = 0, j = 0, tempmax = 0;
    if (myArray.length == 1)
    {
        return max;
    }
    else if (max < myArray[i])
    {
        max = myArray[i];
        int[] tempArray = new int[myArray.length-1];
        for (i = 1; i < myArray.length; i++)
        {
            tempArray[j] = myArray[i];
            j++;
        }
        tempmax = getLargest(tempArray, max);
        return tempmax;
    }
    else if
    {
        int[] tempArray = new int[myArray.length-1];
        for (i = 1; i < myArray.length; i++)
        {
            tempArray[j] = myArray[i];
            j++;
        }
        tempmax = getLargest(tempArray, max);
        return tempmax;
    }
}
}

【问题讨论】:

  • 只是一个小提示。如果您传递上次检查的索引,则不需要在每一步都创建一个新数组。
  • 您确定此代码仅在最后一个元素最大的情况下不起作用?它甚至不为我编译。
  • @Mike - 我认为最后一个else if 应该只是else。我将其归因于发帖时的拼写错误。

标签: java arrays recursion


【解决方案1】:

你的第一个条件就是问题:

if (myArray.length == 1)
{
    return max;
}

替换为:

if (myArray.length == 1)
{
    return myArray[0] > max ? myArray[0] : max;
}

如果数组只有一个元素,则返回前一个最大值。如果 max 是最后一个元素,它将被跳过。

【讨论】:

  • 谢谢。我在发布后看到了错误在哪里,但不确定如何更正。你的答案很完美。
【解决方案2】:

您永远不会评估最终元素 - 您只需在数组大小为 1 时返回 max,因此您永远不会真正检查数组中的最终元素。

另外,一个注释 - 不是每次都创建数组的副本,为什么不在每次递归时简单地将当前索引传递给你的函数?

【讨论】:

    【解决方案3】:

    当数组长度为 1 时,getLargest 不会针对max 测试(单个)数组元素;它只是返回max。这就是为什么它总是跳过最后一个元素。

    顺便说一句,最好将max 初始化为 Integer.MIN_VALUE 而不是任意值 -999。

    另外,一旦你的代码工作起来,它仍然会非常低效。您可以考虑将其发布到codereview.stackexchange.com 以获得反馈。

    【讨论】:

    • 谢谢。我知道这可能不是最好的方法。仍然只是学习 Java 这么基础是我目前完成作业所需要的。但我也会按照你的建议发布。
    【解决方案4】:

    分而治之怎么样?

    private static int findLargest(int lowerLimit, int upperLimit) {
        if (lowerLimit == upperLimit) {
            return temp[lowerLimit];
        } else if (upperLimit - lowerLimit == 1){
            return Math.max(temp[upperLimit], temp[lowerLimit]);
        } else {
            int pivot = (upperLimit - lowerLimit + 1)/2;
            int firstHalf = findLargest(lowerLimit, lowerLimit + pivot);
            int secondHalf = findLargest(upperLimit - pivot , upperLimit);
            return Math.max(firstHalf, secondHalf);
        }
    }
    

    当然 temp 是全局数组。要开始递归,你可以用 findLargest(0,temp.length) 来调用它

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-12-07
      • 1970-01-01
      • 2016-11-26
      • 1970-01-01
      • 2015-11-19
      • 1970-01-01
      • 2016-03-27
      相关资源
      最近更新 更多