LeetCode13--最小栈和用队列实现栈

本文介绍了如何使用辅助栈解决LeetCode中的最小栈问题,并探讨了无额外空间情况下通过变量记录最小值的方法。此外,还讨论了用两个队列或一个队列实现栈的思路,遵循后进先出原则进行操作。
摘要由CSDN通过智能技术生成

从今天开始按照tag刷,首先完成栈这一部分的学习,栈和队列总是密不可分的,所以也会涉及到队列的一些基础。

1.最小栈

//设计一个支持 push ,pop ,top 操作,并能在常数时间内检索到最小元素的栈。 
//
// 
// push(x) —— 将元素 x 推入栈中。 
// pop() —— 删除栈顶的元素。 
// top() —— 获取栈顶元素。 
// getMin() —— 检索栈中的最小元素。 
// 
//
// 
//
// 示例: 
//
// 输入:
//["MinStack","push","push","push","getMin","pop","top","getMin"]
//[[],[-2],[0],[-3],[],[],[],[]]
//
//输出:
//[null,null,null,null,-3,null,0,-2]
//
//解释:
//MinStack minStack = new MinStack();
//minStack.push(-2);
//minStack.push(0);
//minStack.push(-3);
//minStack.getMin();   --> 返回 -3.
//minStack.pop();
//minStack.top();      --> 返回 0.
//minStack.getMin();   --> 返回 -2.
// 
//
// 
//
// 提示: 
//
// 
// pop、top 和 getMin 操作总是在 非空栈 上调用。 
// 
// Related Topics 栈 设计

针对这种,我们一般采用辅助栈的方式来解决,创建一个辅助栈,在里面放当前栈中最小的元素,没push进去一个元素,辅助栈也进行比较,push当前最小的元素进栈。

class MinStack {
    //创建两个栈,一个就是题中要求的栈,一个是辅助栈,辅助栈用于存储当前栈中最小的值
    Deque<Integer> xStack;
    Deque<Integer> minStack;

    /** initialize your data structure here. */
    public MinStack() {
        xStack = new LinkedList<Integer>();
        minStack = new LinkedList<Integer>();
        minStack.push(Integer.MAX_VALUE);
    }
    
    public void push(int x) {
        xStack.push(x);
        minStack.push(Math.min(minStack.peek(), x));
    }
    
    public void pop() {
        xStack.pop();
        minStack.pop();
    }
    
    public int top() {
        return xStack.peek();
    }
    
    public int getMin() {
        return minStack.peek();
    }
}

 还有一种方法就比较复杂,不需要用到其他什么额外空间,我们就设置一个变量,为min,当我们入栈和出栈的时候,min会跟着变化,关键就是如何记录min的变化过程的中间量,这些中间量很重要,因为在出栈的时候,min会变化。所以当我们出现新的最小值时,我们会将min此时的值在新的最小值之前压入栈中,然后再给min赋上新的最小值。于是乎,就有了如下的代码:

class MinStack {
    //创建两个栈,一个就是题中要求的栈,一个是辅助栈,辅助栈用于存储当前栈中最小的值
    Deque<Integer> xStack;
    int min = Integer.MAX_VALUE;

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

    public void push(int x) {
        if(x<=min){
            xStack.push(min);
            min = x;
        }
        xStack.push(x);
    }

    public void pop() {
        //这里指无论怎样都要弹出一个,如果弹出的是最小值,那么
        if(xStack.pop()==min){
            min = xStack.pop();
        }
    }

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

    public int getMin() {
        return min;
    }
}

2.用队列实现栈

//使用队列实现栈的下列操作: 
//
// 
// push(x) -- 元素 x 入栈 
// pop() -- 移除栈顶元素 
// top() -- 获取栈顶元素 
// empty() -- 返回栈是否为空 
// 
//
// 注意: 
//
// 
// 你只能使用队列的基本操作-- 也就是 push to back, peek/pop from front, size, 和 is empty 这些操作是合
//法的。 
// 你所使用的语言也许不支持队列。 你可以使用 list 或者 deque(双端队列)来模拟一个队列 , 只要是标准的队列操作即可。 
// 你可以假设所有操作都是有效的(例如, 对一个空的栈不会调用 pop 或者 top 操作)。 
// 
// Related Topics 栈 设计

fig1

 

class MyStack {

    Queue<Integer> queue1;
    Queue<Integer> queue2;
    /** Initialize your data structure here. */
    public MyStack() {
        queue1 = new LinkedList<Integer>();
        queue2 = new LinkedList<Integer>();
    }
    
    /** Push element x onto stack. */
    public void push(int x) {
        queue2.offer(x);
        while(!queue1.isEmpty()){
            queue2.offer(queue1.poll());
        }
        Queue<Integer> temp = queue1;
        queue1 = queue2;
        queue2 = temp;
    }
    
    /** Removes the element on top of the stack and returns that element. */
    public int pop() {
        return queue1.poll();
    }
    
    /** Get the top element. */
    public int top() {
        return queue1.peek();
    }
    
    /** Returns whether the stack is empty. */
    public boolean empty() {
        return queue1.isEmpty();
    }
}

采用两个队列进行实现,只要我们有数push进来,我们就将原先放在队列1中的全部重新push到2中。主要就是看时间,把握后进先出,先进后出的原则,把最先进队列的offer到最后面。

方法二就是采用一个队列进行操作,方法也大差不差,也是由数push进来依次出队列,再依次进队列。

fig2

class MyStack {

    Queue<Integer> queue1;
    /** Initialize your data structure here. */
    public MyStack() {
        queue1 = new LinkedList<Integer>();
    }
    
    /** Push element x onto stack. */
    public void push(int x) {
        int n = queue1.size();
        queue1.offer(x);
        for (int i = 0; i < n; i++) {
            queue1.offer(queue1.poll());
        }
    }
    
    /** Removes the element on top of the stack and returns that element. */
    public int pop() {
        return queue1.poll();
    }
    
    /** Get the top element. */
    public int top() {
        return queue1.peek();
    }
    
    /** Returns whether the stack is empty. */
    public boolean empty() {
        return queue1.isEmpty();
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值