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());
        }
    }
  • 相关阅读:
    vim 插件之commentary
    vim-进入插入模式快捷键
    adb logcat 使用
    操作excel脚本练习
    python-openpyxl安装
    python-excel操作之xlrd
    adb-端口被占用解决办法(win)
    打不开微信分享的链接
    在BUG分支下创建分支,开发后合并到bus分支
    css缓存问题
  • 原文地址:https://www.cnblogs.com/yanghailu/p/12774890.html
Copyright © 2011-2022 走看看