• 30-Day Leetcoding Challenge Day10


    三种解法:

    第一种:用了Stack<int[x,y]>这样的数据结构,其中y为栈中当前最小值

    第二种:用了两个栈为stack<Integer> 和minstack<Integer>,其中minstack存储stack的当前最小值

    第三种:用了Stack<Integer>和minStack<int [ ]>

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

    注意 ‘==’ 和 equals的区别;equals是判断两个变量或实例指向同一个内存空间的值是不是相同,==是判断两个变量或实例是不是指向同一个内存空间

    class MinStack {
    
        /** initialize your data structure here. */
        private Stack<Integer> stack = new Stack<>();
        private Stack<Integer> minstack = new Stack<>();
        
        //public MinStack(){}
        
        public void push(int x) {
            if(stack.isEmpty() || x <= minstack.peek()){
                minstack.push(x);
            }
            stack.push(x);
        }
        
        public void pop() {
            if(stack.peek().equals(minstack.peek())){ //bug ==比的是引用,equals比的是值
                minstack.pop();
            }
            stack.pop();
        }
        
        public int top() {
            return stack.peek();
        }
        
        public int getMin() {
            return minstack.peek();
        }  
    }
    class MinStack {
    
        /** initialize your data structure here. */
        private Stack<Integer> stack = new Stack<>();
        private Stack<int[]> minstack = new Stack<>();
        
        //public MinStack(){}
        
        public void push(int x) {
            if(stack.isEmpty() || x <= minstack.peek()[0]){
                minstack.push(new int[]{x, 1});
            }
            else if(x == minstack.peek()[0]){
                minstack.peek()[1]++;
            }
            stack.push(x);
        }
        
        public void pop() {
            if(stack.peek().equals(minstack.peek()[0])){ //bug ==比的是引用,equals比的是值
                minstack.peek()[1]--;
            }
            if(minstack.peek()[1] == 0){
                minstack.pop();
            }
            stack.pop();
        }
        
        public int top() {
            return stack.peek();
        }
        
        public int getMin() {
            return minstack.peek()[0];
        }  
    }
  • 相关阅读:
    正则表达式的妙用获得数组
    道不远人,话IO框架
    页面和控件的生命周期及其二者的关系
    深度解析 TypeConverter & TypeConverterAttribute (二)
    今天我顺利的在cnblogs安家,我要在这里写下辉煌
    AJAX or Ajax
    深度解析 TypeConverter & TypeConverterAttribute (一)
    SonarQube简单入门
    vuecli · Failed to download repo vuejstemplates/webpack: connect ETIMEDOUT 192.30.253.112:443
    Sqlmap使用教程
  • 原文地址:https://www.cnblogs.com/yawenw/p/12707213.html
Copyright © 2020-2023  润新知