【问题标题】:How do I find the minimum value in an array using recursion and only one parameter? [closed]如何使用递归和一个参数找到数组中的最小值? [关闭]
【发布时间】:2015-03-09 05:42:23
【问题描述】:

我的老师挑战我们使用递归找到数组中的最小值,但你只能有一个参数,即数组。

public int minimum(int arry[])

【问题讨论】:

  • 好吧。你基本上有2个选择。 1. 在递归传递数组时,使用静态字段来计算正在比较的元素。 2. 通过在每个递归调用中切掉第一个元素来创建一个子数组。
  • @TheLostMind 我强烈反对选项 1,甚至建议将其作为选项的想法。它不符合任务的精神,当然也没有教 OP 如何递归地构建问题(这就是重点)。此外,它本质上与第二个函数参数相同,只是它不是可重入的或线程安全的。
  • @JasonC - 我认为这个问题本身是有缺陷的。理想情况下,使用递归时,您还必须传递 count 字段。否则,就像 Bohemian's 的答案一样,您必须复制整个数组。
  • @TheLostMind 传递当前索引以避免复制数组只是 Java 实现的性能优化。它并不表示问题中存在缺陷,并且与手头的问题无关。
  • @JasonC - 如果您希望答案能够正确地提出递归,请正确地提出问题。我说只允许 int 在问题中传递,然后保持数组全局。这仍然是递归。为什么要混淆OP? .我只是指出老师的问题中的一个缺陷。 :P

标签: java recursion


【解决方案1】:

这些技巧是尝试以递归方式定义问题,即在定义中使用操作本身以及不使用的“基本情况”。例如,在这种情况下,想想“数组中的最小值”是什么:

  • 对于大小为 1 的数组,最小值就是那个元素。
  • 对于较大的数组,最小值是第一个元素和数组其余部分中的最小值中的较小者

所以你有你的基本情况(大小为 1 的数组)和你的递归。这应该足够你继续了。

【讨论】:

  • 好答案,比我的好。 +1
【解决方案2】:

试试下面的代码:

概念:从数组中删除较大的值,当数组中只有一个元素时返回最小值。

public int min(int[] n) {
        if (n.length > 1) {
            int a = n[0];
            int b = n[1];
            int[] newN = new int[n.length - 1];

            for (int i = 0; i < newN.length; i++) {
                if (i == 0)
                    newN[i] = a < b ? a : b;
                else
                    newN[i] = n[i + 1];
            }
            return min(newN);

        }
        return n[0];
    }

【讨论】:

  • 如果你能解释为什么你选择这样做,它可能比更传统的方法有什么好处,以及是什么过程导致你到达这种方法,这个答案会更有用。否则,它无助于 OP 学习任何真正的技能。
【解决方案3】:

您必须将连续较小的数组传递给下一次迭代:

public int minimum(int arr[]) {
    if (arr == null || arr.length == 0)
        throw new IllegalArgumentException();
    if (arr.length == 1)
        return arr[0];
    int min = minimum(Arrays.copyOfRange(arr, 1, arr.length));
    return arr[0] < min ? arr[0] : min;
}

但这只是一个学术练习——“不要在家里尝试这个”。

【讨论】:

  • 不太喜欢 OP 可以复制并粘贴到他的作业中而不必先考虑的答案;尤其是那些只属于这里的帖子。
  • 在家里试试会不会爆炸?
  • @rene 很难想到效率较低的算法
【解决方案4】:
public static int min(int[] n) {
    if(n.length == 1)//base case
        return(n[0]);
    int a = n.length%2 == 0 ? 0:1;      //Awesome sauce syntax http://www.cafeaulait.org/course/week2/43.html
    int[] r =new int[n.length/2 + a];   // reduce by a factor of 2 each iteration
    for(int k = 0 ; k < n.length/2 + a ; k++){    //While copying to a smaller array you might as well make comparisons.
        r[k] = n[k]<=n[n.length-k-1] ? n[k] : n[n.length-k-1];//compare the beginning and end of your array, take the smaller of the two.
    }   //In the case that you have an odd number of elements the middle is always copied trough to the next iteration
    return(min(r));//This is where the recursion happens.
}   // There is always a better way but this should satisfy your teacher.

【讨论】:

  • 忽略发布纯代码答案的所有其他问题,您的“真棒酱语法”(我假设这是您的基本名称 ?: 运算符)n.length % 2 == 0 ? 0 : 1 与只是n.length % 2 一个人。
  • 我需要一行代码来发布链接。我也用了几次,为什么不呢?
  • 呃……什么?我认为您误解了:您不需要在那条线上的? 0 : 1n.length % 2已经为 0 或 1。有关 % 运算符的更多信息,请参阅 cafeaulait.org/course/week2/15.html
【解决方案5】:
    public static int[] removeElement(int element,int[] original){
        int[] n = new int[original.length - 1];
        System.arraycopy(original, 0, n, 0, element );
        System.arraycopy(original, element+1, n, element, original.length - element-1);
        return n;// http://stackoverflow.com/questions/4870188/delete-item-from-array-and-shrink-array
    }
    public static int [] shift(int[] original){
        int[] a = new int[original.length];
        for(int k = 1 ; k < original.length;k++){
            a[k-1] = original[k];
        }
        a[original.length-1] = original[0];
        return(a);
    }
    public static int minimum(int[] arr){ //Process of elimination
        if(arr.length==1){ //Base Case
           return(arr[0]); 
        }
        if(arr[0]>=arr[1]){// reduction step
           return(minimum(removeElement(0,arr)));
        }else{ // tread water
            return(minimum(shift(arr)));
        }
    }// There is always a better way but this sould satisfy your teacher.

给 Pratik Popat 一个赞成票,因为他抄袭了我平庸的逻辑。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-07
    • 2015-09-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多