【问题标题】:Find the minimal value in stack with recursion使用递归查找堆栈中的最小值
【发布时间】:2020-04-09 01:01:03
【问题描述】:

我需要在堆栈中递归地找到最小值,然后返回最小值... 我尝试迭代解决它并且它有效。

我的递归尝试:

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();
    }
}

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

【问题讨论】:

    标签: java recursion data-structures stack min


    【解决方案1】:

    您可以保留原来的Stack如下:

     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;
    }
    

    此递归是通过观察Stack 中的最小值创建的,该最小值是Stack 的顶部和Stack 的最小值之间的最小值,这是通过删除Stack 的顶部得到的。

    通过在每次递归调用后添加弹出元素,我确保Stack 最终返回其原始状态。

    请注意,此解决方案不需要将第二个参数传递给递归方法。

    测试:

    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));
    System.out.println (stack);
    

    输出:

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

    如您所见,Stack 最终包含了它的所有元素。

    【讨论】:

      【解决方案2】:

      这可能被认为是作弊,但您可以通过克隆它并在克隆上操作来避免修改原始堆栈。

      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));
          }
      }
      

      【讨论】:

      • 不错的解决方案!只是缺少保存堆栈元素的部分。谢谢(:
      • @S.O.M 我错过了这个要求。我已经发布了一个更新的解决方案
      猜你喜欢
      • 2014-12-24
      • 2011-11-24
      • 2011-01-30
      • 1970-01-01
      • 2015-05-11
      • 2016-04-06
      • 2012-03-18
      • 1970-01-01
      • 2016-05-07
      相关资源
      最近更新 更多