Day10|LeetCode232.用栈实现队列、LeetCode 225. 用队列实现栈

栈和队列理论基础:

队列是先进先出,栈是先进后出。如图所示:

栈与队列理论1

栈和队列是STL(C++标准库)里面的两个数据结构。

栈是以底层容器完成其所有的工作,对外提供统一的接口,底层容器是可插拔的(也就是说我们可以控制使用哪种容器来实现栈的功能)。 

栈的内部结构,栈的底层实现可以是vector,deque,list 都是可以的, 主要就是数组和链表的底层实现。如图所示:

栈与队列理论3

我们常用的SGI STL,如果没有指定底层实现的话,默认是以deque为缺省情况下栈的底层结构。 

LeetCode 232.用栈实现队列

题目链接:232. 用栈实现队列 - 力扣(LeetCode)

视频链接:栈的基本操作! | LeetCode:232.用栈实现队列_哔哩哔哩_bilibili

思路

用栈来实现队列,仅仅一个栈是不行的,需要用两个栈,一个输入栈,一个输出栈。如图所示:

 

232.用栈实现队列版本2

代码实现

class MyQueue {
public:
    stack<int> stIn;
    stack<int> stOut;
    MyQueue() {

    }
    
    void push(int x) {
        stIn.push(x);

    }
    
    int pop() {
        if(stOut.empty()) {
            while(!stIn.empty()) {
                stOut.push(stIn.top());
                stIn.pop();
            }
        }
        int result = stOut.top();
        stOut.pop();
        return result;

    }
    
    int peek() {
        int res = this->pop();
        stOut.push(res);
        return res;

    }
    
    bool empty() {
        return stIn.empty() && stOut.empty();

    }
};

·时间复杂度:push和empty为O(1), pop和peek为O(n)

·空间复杂度:O(n)

LeetCode 225. 用队列实现栈

题目链接:225. 用队列实现栈 - 力扣(LeetCode)

视频链接:队列的基本操作! | LeetCode:225. 用队列实现栈_哔哩哔哩_bilibili

思路

这道题有两种方法,第一种是两个队列来实现栈,第二种是一个队列来实现栈。

两个队列来实现栈:用两个队列que1和que2实现队列的功能,que2其实完全就是一个备份的作用,把que1最后面的元素以外的元素都备份到que2,然后弹出最后面的元素,再把其他元素从que2导回que1。如图所示:

一个队列来实现栈:一个队列在模拟栈弹出元素的时候只要将队列头部的元素(除了最后一个元素外) 重新添加到队列尾部,此时再去弹出元素就是栈的顺序了。

1、代码实现(两个队列来实现栈)

class MyStack {
public:
    queue<int> que1;
    queue<int> que2; // 辅助队列,用来备份
    /** Initialize your data structure here. */
    MyStack() {

    }

    /** Push element x onto stack. */
    void push(int x) {
        que1.push(x);
    }

    /** Removes the element on top of the stack and returns that element. */
    int pop() {
        int size = que1.size();
        size--;
        while (size--) { // 将que1 导入que2,但要留下最后一个元素
            que2.push(que1.front());
            que1.pop();
        }

        int result = que1.front(); // 留下的最后一个元素就是要返回的值
        que1.pop();
        que1 = que2;            // 再将que2赋值给que1
        while (!que2.empty()) { // 清空que2
            que2.pop();
        }
        return result;
    }

    /** Get the top element. */
    int top() {
        return que1.back();
    }

    /** Returns whether the stack is empty. */
    bool empty() {
        return que1.empty();
    }
};

·时间复杂度:push为O(n),其他为O(1)

·空间复杂度:O(n)

2、代码实现(一个队列来实现栈)

class MyStack {
public:
    queue<int> que;
    MyStack() {

    }
    
    void push(int x) {
        que.push(x);

    }
    
    int pop() {
        int size = que.size();
        size--;
        while(size--) {
            que.push(que.front());
            que.pop();
        }
        int result = que.front();
        que.pop();
        return result;

    }
    
    int top() {
        return que.back();

    }
    
    bool empty() {
        return que.empty();

    }
};

·时间复杂度: push为O(n),其他为O(1)

·空间复杂度: O(n)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值