zoukankan      html  css  js  c++  java
  • [leetCode]155.最小栈

    辅助栈

    使用辅助栈记录栈每个状态的最小值

    class MinStack {
        
        private Stack<Integer> dataStack;
        private Stack<Integer> minStack;
    
        /** initialize your data structure here. */
        public MinStack() {
            dataStack = new Stack<>();
            minStack = new Stack<>();
        }
        
        public void push(int x) {
           dataStack.push(x);
           if(minStack.isEmpty() || x <= minStack.peek()){
               minStack.push(x);
           }
        }
        
        public void pop() {
            int x = dataStack.pop();
            if( x == minStack.peek()){
                minStack.pop();
            }
        }
        
        public int top() {
            return dataStack.peek();
        }
        
        public int getMin() {
            return minStack.peek();
        }
    }
    
    

    stack

    使用自定义数据结构,保存当前元素外还保存最小值

    class MinStack {
        
        private Stack<Node> stack;
    
        class Node{
            int val;
            int min;
            public Node(int x, int m) {
                val = x;
                min = m;
            }
        }
        /** initialize your data structure here. */
        public MinStack() {
            stack = new Stack<>();
        }
        
        public void push(int x) {
            if(stack.isEmpty()){
                stack.push(new Node(x,x));
            }else {
    
                stack.push(new Node(x,Math.min(x,stack.peek().min)));
            }
        }
        
        public void pop() {
            stack.pop();
        }
        
        public int top() {
            return stack.peek().val;
        }
        
        public int getMin() {
            return stack.peek().min;
        }
    }
    
    

    自定义栈

    使用单链表自定义栈,每个结点保存当前栈的最小值

    class MinStack {
    
        private Node first;
    
        class Node{
            int val;
            int min;
            Node next;
            public Node(int x, int m, Node n) {
                val = x;
                min = m;
                next = n;
            }
        }
        /** initialize your data structure here. */
        public MinStack() {
           
        }
        
        public void push(int x) {
            Node lastFirst = first;
            if(first == null){
                 first = new Node(x, x, lastFirst);
            }else{
                first = new Node(x, 
                Math.min(first.min,x),lastFirst);
            }
           
        }
        
        public void pop() {
            first = first.next;
        }
        
        public int top() {
            return first.val;
        }
        
        public int getMin() {
            return first.min;
        }
    }
    
  • 相关阅读:
    git在eclipse中的配置 转载
    Java annotation 自定义注释@interface的用法 转载记录
    Java内存溢出的详细解决方案 转载
    sql server 分页、存储过程、视图
    重新认识Asp.Net管道模型
    vim修改替换
    Excel hong
    开始第一次Delphi
    常用正则表达式的网站
    := 赋值语句
  • 原文地址:https://www.cnblogs.com/PythonFCG/p/13859999.html
Copyright © 2011-2022 走看看