zoukankan      html  css  js  c++  java
  • 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];
        }  
    }
  • 相关阅读:
    设计手稿: 搜索引擎
    软件版本介绍
    VS2012中使用编译的Qt-5.1.1静态库开发程序
    POJ2236(并查集)
    Java关键字this的用法总结
    paip.提升用户体验-----c++ gcc 命令在notepad++扩展中的配置..
    MySQL基本查询语句练习
    [置顶] 提升代码内外部质量的22条经验
    mysql 数据库复制表 create table city1 like city;
    两个脚本
  • 原文地址:https://www.cnblogs.com/yawenw/p/12707213.html
Copyright © 2011-2022 走看看