代码随想录算法训练营第10天-232.用栈实现队列-225. 用队列实现栈

232.用栈实现队列

1.自己的想法

​ 我的想法很简单,用另一个栈作为辅助空间,负责将原本的栈颠倒过来

class MyQueue {
public:
    MyQueue() {

    }
    
    void push(int x) {
        st1.push(x);
    }
    
    int pop() {
        while (!st1.empty())
        {
            st2.push(st1.top());
            st1.pop();
        }

        int tmp = st2.top();
        st2.pop();

        while (!st2.empty())
        {
            st1.push(st2.top());
            st2.pop();
        }

        return tmp;
    }
    
    int peek() {
        while (!st1.empty())
        {
            st2.push(st1.top());
            st1.pop();
        }

        int tmp = st2.top();
        
        while (!st2.empty())
        {
            st1.push(st2.top());
            st2.pop();
        }

        return tmp;
    }
    
    bool empty() {
        return st1.empty();
    }
private:
    stack<int> st1; //作为队列
    stack<int> st2; //作为辅助
};

2.代码随想录提供的思路,一个输入栈,一个输出栈

class MyQueue {
public:
    MyQueue() {

    }
    
    void push(int x) {
        st1.push(x);
    }
    
    int pop() {
        if (st2.empty())
        {
            while (!st1.empty())
            {
                st2.push(st1.top());
                st1.pop();
            }
        }
        int res = st2.top();
        st2.pop();
        return res;
    }
    
    int peek() {
        int res = this->pop();
        st2.push(res);
        return res;
    }
    
    bool empty() {
        return st1.empty() && st2.empty();
    }
private:
    stack<int> st1; //作为输入栈
    stack<int> st2; //作为输出栈
};

225. 用队列实现栈

1.我自己想的

我的想法:每次入队时都用辅助队列帮其挪到队头。

class MyStack {
public:
    MyStack() {

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

        while (!q1.empty())
        {
            q2.push(q1.front());
            q1.pop();
        }

        while (!q2.empty())
        {
            q1.push(q2.front());
            q2.pop();
        }
    }
    
    int pop() {
        int res = q1.front();
        q1.pop();
        return res;
    }
    
    int top() {
        int res = q1.front();
        return res;
    }
    
    bool empty() {
        return q1.empty();
    }

private:
    queue<int> q1;  //以其为栈
    queue<int> q2;  //辅助空间
};

2.代码随想录版本,可以把尾部前的元素全部重新入队

class MyStack {
public:
    MyStack() {

    }
    
    void push(int x) {
        q.push(x);
    }
    
    int pop() {
        int n = q.size() - 1;
        while (n--)
        {
            q.push(q.front());
            q.pop();
        }

        int res = q.front();
        q.pop();
        return res;
    }
    
    int top() {
        return q.back();
    }
    
    bool empty() {
        return q.empty();
    }

private:
    queue<int> q;  //以其为栈
};
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

去人777

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

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

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

打赏作者

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

抵扣说明:

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

余额充值