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;
        }
    }
    
  • 相关阅读:
    Oracle RMAN快速入门指南
    防止SQL注入
    脚本获取参数 
    回车下一个
    DataTable
    C# Eval
    C#过滤所有HTML代码的函数
    反射
    gridview格式化问题
    绑定
  • 原文地址:https://www.cnblogs.com/PythonFCG/p/13859999.html
Copyright © 2011-2022 走看看