[栈和队列简单题] LeetCode 232. 用栈实现队列,225. 用队列实现栈

该篇博客探讨了如何使用两个栈实现一个高效的FIFO(先入先出)队列,以及如何利用两个队列实现FILO(后入先出)的栈。在栈的实现中,入栈操作时间复杂度为O(1),出栈为O(n);而在队列的实现中,入队操作时间复杂度为O(n),出队为O(1)。两种实现都通过巧妙地数据迁移策略确保了操作的正确性和效率。
摘要由CSDN通过智能技术生成

LeetCode 232. 用栈实现队列

https://leetcode.cn/problems/implement-queue-using-stacks
请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作(pushpoppeekempty):

实现 MyQueue 类:

void push(int x) 将元素 x 推到队列的末尾
int pop() 从队列的开头移除并返回元素
int peek() 返回队列开头的元素
boolean empty() 如果队列为空,返回 true ;否则,返回 false

前提条件:假设操作均合法。

Solution

出队时间复杂度为 O ( n ) O(n) O(n), 入队为 O ( 1 ) O(1) O(1)

stack1 用于加入数据,stack2 用于取数据。

当取数据时若 stack2 为空,则从 stack1 依次取出数据放入 stack2

class MyQueue {
    private Stack<Integer> stack1 = new Stack<Integer>();
    private Stack<Integer> stack2 = new Stack<Integer>();

    public MyQueue() {

    }
    
    public void push(int x) {
        stack1.push(x);
    }
    
    public int pop() {
        if (stack2.empty()) {
            while (!stack1.empty()) {
                stack2.push(stack1.pop());
            }
        }
        return stack2.pop();
    }
    
    public int peek() {
        if (stack2.empty()) {
            while (!stack1.empty()) {
                stack2.push(stack1.pop());
            }
        }
        return stack2.peek();
    }
    
    public boolean empty() {
        return stack1.empty() && stack2.empty();
    }
}

LeetCode 225. 用队列实现栈

https://leetcode.cn/problems/implement-stack-using-queues
请你仅使用两个队列实现一个后入先出(LIFO)的栈,并支持普通栈的全部四种操作(pushtoppopempty)。

实现 MyStack 类:

void push(int x) 将元素 x 压入栈顶。
int pop() 移除并返回栈顶元素。
int top() 返回栈顶元素。
boolean empty() 如果栈是空的,返回 true ;否则,返回 false

Solution

出栈时间复杂度为 O ( 1 ) O(1) O(1), 入栈为 O ( n ) O(n) O(n)

入栈操作实现:先入 queue1,然后将 queue2 的数据依次倒入 queue1,然后交换 queue1queue2,交换后 queue1 为空。

出栈操作和取栈顶元素操作实现:直接取 queue2 的队头即可。

判空实现:queue2 是否为空。

队列判空用 isEmpty()

class MyStack {
    private Queue<Integer> queue1 = new LinkedList<Integer>();
    private Queue<Integer> queue2 = new LinkedList<Integer>();

    public MyStack() {

    }
    
    public void push(int x) {
        queue1.offer(x);  // 有些队列有大小限制,若队满,调用 add() 方法抛出异常, 而 offer() 返回 false
        // 将 queue2 的数据倒入 queue1
        while (!queue2.isEmpty()) { 
            queue1.offer(queue2.poll());
        }
        Queue temp = queue1;
        queue1 = queue2;
        queue2 = temp;
    }
    
    public int pop() {
        return queue2.poll();  // poll() 方法在用空集合调用时不是抛出异常,只是返回 null, remove() 方法会报异常
    }
    
    public int top() {
        return queue2.peek(); // 在队列为空时, element() 抛出一个异常,而 peek() 返回 null
    }
    
    public boolean empty() {
        return queue2.isEmpty();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

哇咔咔负负得正

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值