[Leetcode] 225. Implement Stack using Queues 解题报告

本文介绍如何使用队列实现栈的基本操作,包括push、pop、top及empty等,并提供了懒惰实现与勤奋实现两种方法的具体代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目

Implement the following operations of a stack using queues.

  • push(x) -- Push element x onto stack.
  • pop() -- Removes the element on top of the stack.
  • top() -- Get the top element.
  • empty() -- Return whether the stack is empty.
Notes:
  • You must use only standard operations of a queue -- which means only push to backpeek/pop from frontsize, and is empty operations are valid.
  • Depending on your language, queue may not be supported natively. You may simulate a queue by using a list or deque (double-ended queue), as long as you use only standard operations of a queue.
  • You may assume that all operations are valid (for example, no pop or top operations will be called on an empty stack).

思路

我们首先看看队列和栈的区别:队列是先进先出,而栈是后进先出。可以看出算法实现的关键就在于在队列中存储元素的时候,将最近push进来的元素放在队列首位还是末尾了。根据这个区别,就可以有两种实现方法:

1、懒惰实现:顾名思义,就是新加入一个元素之后,我们懒惰第将它放在队列末尾。这就导致一个问题,当需要pop的时候,我们此时需要pop掉的是队列中的最后一个元素,而这个操作是队列这种数据结构所不支持的。所以在懒惰实现的pop中,我们需要首先把除队末元素之外的所有队列元素都移出队列,然后重新添加,这样就可以保证原来的队末元素此时位于队列头部了,可以直接pop。

2、勤奋实现:顾名思义,就是新push一个元素进来的时候,我们想办法把它置于队列头部,这样当需要pop的时候直接pop出来队列头部就可以了。所以这种“勤奋实现”的关键在于push函数的实现。事实上它和“懒惰实现”的pop函数完全一样:先把新元素加在队列元素的末尾,然后把所有除此之外的元素顺次移除,并添加到队列的末尾。

代码

1、懒惰实现:

class MyStack {
public:
    /** Initialize your data structure here. */
    MyStack() {
        
    }
    
    /** Push element x onto stack. */
    void push(int x) {
        que.push(x);
    }
    
    /** Removes the element on top of the stack and returns that element. */
    int pop() {
        int size = que.size();
        for (int i = 0; i < size - 1; ++i) {
            que.push(que.front());
            que.pop();
        }
        int ret = que.front();
        que.pop();
        return ret;
    }
    
    /** Get the top element. */
    int top() {
        return que.back();
    }
    
    /** Returns whether the stack is empty. */
    bool empty() {
        return que.empty();
    }
private:
    queue<int> que;
};

/**
 * Your MyStack object will be instantiated and called as such:
 * MyStack obj = new MyStack();
 * obj.push(x);
 * int param_2 = obj.pop();
 * int param_3 = obj.top();
 * bool param_4 = obj.empty();
 */

2、勤奋实现:

class MyStack {
public:
    /** Initialize your data structure here. */
    MyStack() {
        
    }
    
    /** Push element x onto stack. */
    void push(int x) {
        que.push(x);
        for (int i = 0; i < que.size() - 1; ++i) {
            que.push(que.front());
            que.pop();
        }
    }
    
    /** Removes the element on top of the stack and returns that element. */
    int pop() {
        int ret = que.front();
        que.pop();
        return ret;
    }
    
    /** Get the top element. */
    int top() {
        return que.front();
    }
    
    /** Returns whether the stack is empty. */
    bool empty() {
        return que.empty();
    }
private:
    queue<int> que;
};

/**
 * Your MyStack object will be instantiated and called as such:
 * MyStack obj = new MyStack();
 * obj.push(x);
 * int param_2 = obj.pop();
 * int param_3 = obj.top();
 * bool param_4 = obj.empty();
 */
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值