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;
        }
    }
    
  • 相关阅读:
    一个好用的小图标库
    前端应用笔记
    HttpClient的使用今天遇到一个巨坑——HttpEntity内容取不出来
    HttpClient忽略SSL证书
    SpringBoot+Thyemelaf开发环境正常,打包jar发到服务器就报错Template might not exist or might not be accessible
    查询Mysql数据库所有数据库所占磁盘空间大小
    Docker安装并运行mysql5.6数据库
    Docker下载镜像太慢问题
    Docker安装Nginx
    vue-router导航守卫
  • 原文地址:https://www.cnblogs.com/PythonFCG/p/13859999.html
Copyright © 2011-2022 走看看