• [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;
        }
    }
    
  • 相关阅读:
    「SPOJ10707」Count on a tree II
    UVA 11021 /概率
    power oj/2360/Change
    POJ1613 147/思维题
    Power oj2498/DP/递推
    HDU4815/计数DP
    444A/CF
    观光公交noip<贪心>
    2014 Shanghai Invitation Contest
    POJ1734/Floyd求最小环
  • 原文地址:https://www.cnblogs.com/PythonFCG/p/13859999.html
Copyright © 2020-2023  润新知