Java 使用递归查找堆栈中的最小值

Java 使用递归查找堆栈中的最小值,java,recursion,data-structures,stack,min,Java,Recursion,Data Structures,Stack,Min,我需要在堆栈中递归地找到最小值,然后返回最小值。。。 我试着用迭代的方法来解决这个问题,结果成功了 我的递归尝试: import java.util.Scanner; public class Main { public static int minimalValue(Stack<Integer> s, int min) { if (s.isEmpty()) return min; // Save the previ

我需要在堆栈中递归地找到最小值,然后返回最小值。。。 我试着用迭代的方法来解决这个问题,结果成功了

我的递归尝试:

import java.util.Scanner;

public class Main {

    public static int minimalValue(Stack<Integer> s, int min) {
        if (s.isEmpty())
            return min;
        // Save the previous value.
        int prev = s.pop();

        if (!s.isEmpty() && prev < min)
            min = prev;
        return minimalValue(s, min);
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        Stack<Integer> stack = new Stack<Integer>();
        stack.push(99);
        stack.push(64);
        stack.push(73);
        stack.push(54);
        stack.push(100);
        stack.push(82);
        stack.push(55);
        stack.push(300);
        System.out.println(minimalValue(stack, stack.top()));

        in.close();
    }
}
如您所见,在方法完成后,堆栈中的每个元素都被删除。
您能给我一个更好的解决方案吗?

您可以保留原始堆栈,如下所示:

 public static int minimalValue(Stack<Integer> s) {
    if (s.isEmpty())
        return Integer.MAX_VALUE;
    int last = s.pop();
    int min = Math.min(last, minimalValue(s));
    s.push(last);
    return min;
}
输出:

54
[99, 64, 73, 54, 100, 82, 55, 300]

如您所见,堆栈最终包含其所有元素。

您可以按如下方式保留原始堆栈:

 public static int minimalValue(Stack<Integer> s) {
    if (s.isEmpty())
        return Integer.MAX_VALUE;
    int last = s.pop();
    int min = Math.min(last, minimalValue(s));
    s.push(last);
    return min;
}
输出:

54
[99, 64, 73, 54, 100, 82, 55, 300]

正如您所见,堆栈最终包含其所有元素。

这可能被认为是欺骗,但您可以通过克隆并对克隆进行操作来避免修改原始堆栈

import java.util.Optional;
import java.util.Stack;

class Scratch {
    public static Optional<Integer> minimalValue(Stack<Integer> s, Optional<Integer> min) {
        if (s.empty()) {
            return min;
        }

        Integer topElement = s.pop();
        Integer newMin = Math.min(topElement, min.orElse(topElement));
        return minimalValue(s, Optional.of(newMin));
    }

    public static void main(String[] args) {
        Stack<Integer> stack = new Stack<>();
        stack.push(99);
        stack.push(64);
        stack.push(73);
        stack.push(54);
        stack.push(100);
        stack.push(82);
        stack.push(55);
        stack.push(300);

        Stack<Integer> clone = (Stack) stack.clone();
        Optional<Integer> result = minimalValue(clone, Optional.empty());
        System.out.println(result.orElse(null));
    }
}

这可能被认为是欺骗,但您可以通过克隆原始堆栈并对其进行操作来避免修改原始堆栈

import java.util.Optional;
import java.util.Stack;

class Scratch {
    public static Optional<Integer> minimalValue(Stack<Integer> s, Optional<Integer> min) {
        if (s.empty()) {
            return min;
        }

        Integer topElement = s.pop();
        Integer newMin = Math.min(topElement, min.orElse(topElement));
        return minimalValue(s, Optional.of(newMin));
    }

    public static void main(String[] args) {
        Stack<Integer> stack = new Stack<>();
        stack.push(99);
        stack.push(64);
        stack.push(73);
        stack.push(54);
        stack.push(100);
        stack.push(82);
        stack.push(55);
        stack.push(300);

        Stack<Integer> clone = (Stack) stack.clone();
        Optional<Integer> result = minimalValue(clone, Optional.empty());
        System.out.println(result.orElse(null));
    }
}

很好的解决方案!只缺少保存堆栈元素的部分。thx:@S.O.M我错过了那个要求。我已经发布了一个更新的解决方案很好的解决方案!只缺少保存堆栈元素的部分。thx:@S.O.M我错过了那个要求。我已经发布了一个更新的解决方案