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;
        }
    }
    
  • 相关阅读:
    博客园开通新随笔
    遍历两个数组,并输出数组中的不同内容
    1021-二叉树复制和左右子树互换
    1020-层次遍历二叉树
    1019-计算二叉树的高度和结点数
    1018-深度遍历二叉树
    1017-乘积最大
    1016-求幂
    1015-最大公约数和最小公倍数
    1014-数据的插入与删除
  • 原文地址:https://www.cnblogs.com/PythonFCG/p/13859999.html
Copyright © 2011-2022 走看看