(两百二十一)leetcode 最小栈

1.题目

https://leetcode-cn.com/problems/min-stack/

 

2.思路

push和pop本来就是栈的特性,top查了下有peek方法,主要是最小元素没有现成的,我本来想的方法是用一个辅助栈,每次遍历一下得到最小的再将pop出来的元素放回去,然后超时了。。。

 

3.coding

3.1 超时

class MinStack2 {
    Stack<Integer> mStack;

    /**
     * initialize your data structure here.
     */
    public MinStack2() {
        mStack = new Stack<Integer>();
    }

    public void push(int x) {
        mStack.push(x);
    }

    public void pop() {
        mStack.pop();
    }

    public int top() {
        return mStack.peek();
    }

    public int getMin() {
        Stack<Integer> stack = new Stack<Integer>();
        int min = Integer.MAX_VALUE;
        while(!mStack.isEmpty()){
            int i = mStack.pop();
            if (i < min){
                min = i;
            }
            stack.push(i);
        }
        while(!stack.isEmpty()){
            mStack.push(stack.pop());
        }
        return min;
    }
}

 

3.2 辅助栈保存最小值

class MinStack {
    Stack<Integer> mStack;
    Stack<Integer> mStack2;

    /**
     * initialize your data structure here.
     */
    public MinStack() {
        mStack = new Stack<Integer>();
        mStack2 = new Stack<Integer>();
    }

    public void push(int x) {
        mStack.push(x);
        if (mStack2.isEmpty() || mStack2.peek() >= x) {
            mStack2.push(x);
        } else {
            mStack2.push(mStack2.peek());
        }
    }

    public void pop() {
        mStack.pop();
        mStack2.pop();
    }

    public int top() {
        return mStack.peek();
    }

    public int getMin() {
        return mStack2.peek();
    }
}

mStack2保留最小值,只有小于当前栈顶的值才有价值保存下来,否则没必要保存

 

3.3 辅助栈保存最小值优化

class MinStack {
    Stack<Integer> mStack;
    Stack<Integer> mStack2;

    /**
     * initialize your data structure here.
     */
    public MinStack() {
        mStack = new Stack<Integer>();
        mStack2 = new Stack<Integer>();
    }

    public void push(int x) {
        mStack.push(x);
        if (mStack2.isEmpty() || mStack2.peek() >= x) {
            mStack2.push(x);
        }// else {
            //mStack2.push(mStack2.peek());
        //}
    }

    public void pop() {
        int i = mStack.pop();
        //mStack2.pop();
        if (i == mStack2.peek()) {
            mStack2.pop();
        }
    }

    public int top() {
        return mStack.peek();
    }

    public int getMin() {
        return mStack2.peek();
    }
}

优化了不重复存入最小值

 

3.4 单个栈

class MinStack {
    int min = Integer.MAX_VALUE;
    Stack<Integer> stack = new Stack<Integer>();
    public void push(int x) {
        //当前值更小
        if(x <= min){   
            //将之前的最小值保存
            stack.push(min);
            //更新最小值
            min=x;
        }
        stack.push(x);
    }

    public void pop() {
        //如果弹出的值是最小值,那么将下一个元素更新为最小值
        if(stack.pop() == min) {
            min=stack.pop();
        }
    }

    public int top() {
        return stack.peek();
    }

    public int getMin() {
        return min;
    }
}

作者:windliang
链接:https://leetcode-cn.com/problems/min-stack/solution/xiang-xi-tong-su-de-si-lu-fen-xi-duo-jie-fa-by-38/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

这个很6了,将最小值一起保存在栈里面,但其实思路和双栈并没有区别,只是将两个栈合并成一个栈存储了

 

3.5 链表

class MinStack {
    class Node{
        int value;
        int min;
        Node next;

        Node(int x, int min){
            this.value=x;
            this.min=min;
            next = null;
        }
    }
    Node head;
    //每次加入的节点放到头部
    public void push(int x) {
        if(null==head){
            head = new Node(x,x);
        }else{
            //当前值和之前头结点的最小值较小的做为当前的 min
            Node n = new Node(x, Math.min(x,head.min));
            n.next=head;
            head=n;
        }
    }

    public void pop() {
        if(head!=null)
            head =head.next;
    }

    public int top() {
        if(head!=null)
            return head.value;
        return -1;
    }

    public int getMin() {
        if(null!=head)
            return head.min;
        return -1;
    }
}

作者:windliang
链接:https://leetcode-cn.com/problems/min-stack/solution/xiang-xi-tong-su-de-si-lu-fen-xi-duo-jie-fa-by-38/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

 

 

相关推荐
©️2020 CSDN 皮肤主题: 鲸 设计师:meimeiellie 返回首页