【LeetCode 23】232.用栈实现队列

简介: 【LeetCode 23】232.用栈实现队列

一、题意

二、思路

这是一道模拟题,不涉及到具体算法,考察的是对栈和队列的掌握。

使用栈来模拟队列的行为,只用一个栈是不行的,一定需要两个栈:

  1. 输入栈 stack_in
  2. 输出栈 stack_out
stack<int> stIn;//输入栈
 stack<int> stOut;//输出栈

push函数的时候,只要数据放进去就好了。

/** Push element x to the back of queue. */
    void push(int x) {
        stIn.push(x);
    }

pop函数的时候,操作就复杂一些,输出栈如果为空,就把进栈数据全部导入进来,再从出栈弹出数据,如果输出栈不为空,则直接从出栈弹出数据就可以。

/** Removes the element from in front of queue and returns that element. */
    int pop() {
        // 只有当stOut为空的时候,再从stIn里导入数据(导入stIn全部数据)
        if (stOut.empty()) {
            // 从stIn导入数据直到stIn为空
            while(!stIn.empty()) {
                stOut.push(stIn.top());
                stIn.pop();
            }
        }
        int result = stOut.top();
        stOut.pop();
        return result;
    }

最后如何判断队列为空呢?

如果进栈和出栈都为空的话,说明模拟队列为空了。

/** Returns whether the queue is empty. */
    bool empty() {
        return stIn.empty() && stOut.empty();
    }

返回队列首部元素 peekpop函数的实现是相似的:

/** Get the front element. */
    int peek() {
        int res = this->pop(); // 直接使用已有的pop函数
        stOut.push(res);//上一个pop函数中已将这个元素删除;这里应该push回去,才能保证不删除;
        return res;
    }

三、完整代码

class MyQueue {
public:
    stack<int> stIn;//输入栈
    stack<int> stOut;//输出栈
    /** Initialize your data structure here. */
    MyQueue() {
    }
    /** Push element x to the back of queue. */
    void push(int x) {
        stIn.push(x);
    }
    /** Removes the element from in front of queue and returns that element. */
    int pop() {
        // 只有当stOut为空的时候,再从stIn里导入数据(导入stIn全部数据)
        if (stOut.empty()) {
            // 从stIn导入数据直到stIn为空
            while(!stIn.empty()) {
                stOut.push(stIn.top());
                stIn.pop();
            }
        }
        int result = stOut.top();
        stOut.pop();
        return result;
    }
    /** Get the front element. */
    int peek() {
        int res = this->pop(); // 直接使用已有的pop函数,this应该是队首
        stOut.push(res); // 因为pop函数弹出了元素res,所以再添加回去
        return res;
    }
    /** Returns whether the queue is empty. */
    bool empty() {
        return stIn.empty() && stOut.empty();
    }
};

文章知识点与官

目录
相关文章
|
3月前
|
存储 算法 测试技术
力扣经典150题第五十四题:最小栈
力扣经典150题第五十四题:最小栈
31 0
|
4月前
|
存储 算法 索引
力扣每日一题 6/24 模拟 数组 单调栈
力扣每日一题 6/24 模拟 数组 单调栈
29 0
|
3天前
【LeetCode 24】225.用队列实现栈
【LeetCode 24】225.用队列实现栈
6 0
|
2月前
|
Python
【Leetcode刷题Python】剑指 Offer 30. 包含min函数的栈
本文提供了实现一个包含min函数的栈的Python代码,确保min、push和pop操作的时间复杂度为O(1)。
21 4
|
2月前
|
Python
【Leetcode刷题Python】946. 验证栈序列
LeetCode题目“946. 验证栈序列”的Python解决方案,通过模拟栈的压入和弹出操作来验证给定的两个序列是否能通过合法的栈操作得到。
23 6
|
2月前
|
Python
【Leetcode刷题Python】剑指 Offer 09. 用两个栈实现队列
使用两个栈实现队列的Python解决方案,包括初始化两个栈、实现在队列尾部添加整数的appendTail方法和在队列头部删除整数的deleteHead方法,以及相应的示例操作。
36 2
|
2月前
|
Python
【Leetcode刷题Python】641.循环双端队列
文章介绍了如何实现一个循环双端队列,包括其操作如插入、删除、获取队首和队尾元素,以及检查队列是否为空或已满,并提供了Python语言的实现代码。
21 0
|
2月前
|
Python
【Leetcode刷题Python】232. 用栈实现队列
如何使用Python语言通过两个栈来实现队列的所有基本操作,包括入队(push)、出队(pop)、查看队首元素(peek)和判断队列是否为空(empty),并提供了相应的代码实现。
17 0
|
4月前
|
存储 算法 Python
二刷力扣--栈和队列
二刷力扣--栈和队列
|
3月前
|
Python
155. 最小栈 力扣 python 空间换时间 o(1) 腾讯面试题
155. 最小栈 力扣 python 空间换时间 o(1) 腾讯面试题