zoukankan      html  css  js  c++  java
  • 返回栈中最小元素的两种实现O(1)

    package arithmetic;
    
    import java.util.Stack;
    
    public class GetMinStack {
    
        //省空间,费时间
        public static class MyStack1 {
            private Stack<Integer> stackData;
            private Stack<Integer> stackMin;
    
            public MyStack1() {
                stackData = new Stack<>();
                stackMin = new Stack<>();
            }
    
            public void push(int value) {
                if (this.stackMin.isEmpty()) {
                    stackMin.push(value);
                } else if (value <= getmin()) {
                    stackMin.push(value);
                }
                this.stackData.push(value);
            }
    
            public int pop() {
                if (this.stackData.isEmpty()) {
                    throw new RuntimeException("Your stack is empty.");
                }
                int res = this.stackData.pop();
                if (res == getmin()) {
                    this.stackMin.pop();
                }
                return res;
            }
    
            public int getmin() {
                if (this.stackMin.isEmpty()) {
                    throw new RuntimeException("Your stack is empty.");
                }
                return this.stackMin.peek();
            }
        }
    
        //省时间,费空间
        public static class MyStack2 {
            private Stack<Integer> stackData;
            private Stack<Integer> stackMin;
    
            public MyStack2() {
                this.stackData = new Stack<Integer>();
                this.stackMin = new Stack<Integer>();
            }
    
            public void push(int value) {
                if (stackMin.isEmpty()) {
                    stackMin.push(value);
                }
                if (value <= getmin()) {
                    stackMin.push(value);
                } else {
                    stackMin.push(getmin());
                }
                stackData.push(value);
            }
    
            public int pop() {
                if (this.stackData.isEmpty()) {
                    throw new RuntimeException("Your stack is empty.");
                }
                stackMin.pop();
                return stackData.pop();
            }
    
            public int getmin() {
                if (this.stackMin.isEmpty()) {
                    throw new RuntimeException("Your stack is empty.");
                }
                return this.stackMin.peek();
            }
        }
    
        public static void main(String[] args) {
            MyStack1 stack1 = new MyStack1();
            stack1.push(3);
            System.out.println(stack1.getmin());
            stack1.push(4);
            System.out.println(stack1.getmin());
            stack1.push(1);
            System.out.println(stack1.getmin());
            System.out.println(stack1.pop());
            System.out.println(stack1.getmin());
    
            System.out.println("=============");
    
            MyStack1 stack2 = new MyStack1();
            stack2.push(3);
            System.out.println(stack2.getmin());
            stack2.push(4);
            System.out.println(stack2.getmin());
            stack2.push(1);
            System.out.println(stack2.getmin());
            System.out.println(stack2.pop());
            System.out.println(stack2.getmin());
        }
    }
  • 相关阅读:
    常用FPGA功能块记录
    鸿蒙相关
    微波相关
    Python库大全
    C#环境实现代码的自动生成编译
    STM32相关
    硬件相关
    C# 获取枚举中文注释
    C# 获取自定义特性值
    Asp.Net Core 中 Host 与 WebHost的区别
  • 原文地址:https://www.cnblogs.com/yanghailu/p/12774890.html
Copyright © 2011-2022 走看看