[Leetcode] Implement Stack using Queues 用队列实现栈

news/2024/7/3 1:55:26

双队列法

复杂度

时间 O(N) 空间 O(N)

思路

和Implement Queue using Stack类似,我们也可以用两个队列来模拟栈的操作。当push时,我们将数字offer进非空的队列就行了。当pop时,因为要拿的是队列最后一个数,我们先将它前面的数offer进另一个队列,然后单独拿出最后一个数,并且不再offer进另一个队列,这样,另一个队列就少了最后一个数,而我们也拿到了最后一个数,而本来的队列就变成空了,等待下次的pop操作来填满。top操作和pop一样,区别在于我们拿到最后一个数后,还要再把它offer进另一个队列中。

代码

class MyStack {
    
    Queue<Integer> queue1;
    Queue<Integer> queue2;
    int size;
    
    public MyStack(){
        this.queue1 = new LinkedList<Integer>();
        this.queue2 = new LinkedList<Integer>();
        this.size = 0;
    }
    
    // Push element x onto stack.
    public void push(int x) {
        if(queue1.isEmpty()){
            queue2.offer(x);
        } else {
            queue1.offer(x);
        }
        size++;
    }

    // Removes the element on top of the stack.
    public int pop() {
        if(size == 0){
            return -1;
        }
        int res = 0;
        // 将前面的数都offer进另一个队列,然后拿出最后一个数
        if(queue1.isEmpty()){
            for(int i = 0; i < size - 1; i++){
                queue1.offer(queue2.poll());
            }
            res = queue2.poll();
        } else {
            for(int i = 0; i < size - 1; i++){
                queue2.offer(queue1.poll());
            }
            res = queue1.poll();
        }
        size--;
        return res;
    }

    // Get the top element.
    public int top() {
        if(size == 0){
            return -1;
        }
        // 先执行pop
        int top = pop();
        // 然后将pop出来的数再塞回队列就行了
        if(queue1.isEmpty()){
            queue2.offer(top);
        } else {
            queue1.offer(top);
        }
        // 注意size也要加1
        size++;
        return top;
    }

    // Return whether the stack is empty.
    public boolean empty() {
        return size == 0;
    }
}

http://www.niftyadmin.cn/n/2459968.html

相关文章

组件、接口、类、对象之间的关系

原文地址连接&#xff1a;http://www.cppblog.com/cforce/archive/2012/07/06/181972.aspx 什么是组件 个人的理解&#xff0c;组件是为了实现某个功能而整合在一起的方法及数据的集合&#xff0c;为了描述组件的特征组件中还包含一些描述信息&#xff0c;诸如组件的名称或ID&a…

SpringMVC:返回JSON出现406

1.检查是否加了jackson包 <dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-core</artifactId><version>2.0.1</version> </dependency><!-- https://mvnrepository.com/artifact/com.faste…

html页面显示div源代码:用xmp/xmp标签

html页面显示div源代码&#xff1a;用<xmp></xmp>标签效果还可以。

JAVA: Callable

public interface Callable { /** * Computes a result, or throws an exception if unable to do so. * * return computed result * throws Exception if unable to compute a result */ V call() throws Exception; }

java16:构造器 继承

默认构造器类中一定有构造器如果类没有声明构造器&#xff0c;java编译器提供默认构造器如果类中声明了构造器,java不在提供默认构造器java 根据 参数 去找 对应构造器 package day16;public class Demo01 {public static void main(String[] args) {dog wangcai new dog();//…

Hibernate: Damain对象类若没有无参构造函数 可能发生Javassist Enhancement failed

Domain对象类若没有无参构造函数 可能发生Javassist Enhancement failed

0-100以内的质数

#include <stdio.h> int prim(int n){int flag0;for(int a2;a<n;a){if(n%a0){flag1;break;}}return flag; } int main(){int id;for(int i1;i<100;i){idprim(i);if(id0){printf("%d是质数\n",i);}} }用函数调用的方式实现了0-100内的质数判断并输出

与近似比固定算法的高性能算法

我不得不这样做研究&#xff0c;写论文。我喜欢用这个词来形容提到的高性能算法&#xff0c;感觉有点王婆卖瓜。当然&#xff0c;研究了算法的性能还是不错的&#xff0c;能是否是一个高性能的。自己不肯定的说。最近翻阅读Vazirani的《Approximate Algorithms》一本书。仔细重…