【栈与队列1】232.用栈实现队列 225. 用队列实现栈

232.用栈实现队列

题目链接

(1)文字讲解:https://programmercarl.com/0232.用栈实现队列.html#思路
(2)视频讲解:
(3)题目链接:https://leetcode.cn/problems/implement-queue-using-stacks/

看到题解之前的想法

不会写,已经把栈和队列的所有知识全都忘了。

看到题解之后的想法

栈自带的方法:
push 把一个新的元素推入栈变成新的栈顶
pop 把栈顶的元素推出栈
top 获取栈顶元素值
本题需要模拟的队列的方法:
push(x) – 将一个元素放入队列的尾部。
pop() – 从队列首部移除元素。
peek() – 返回队列首部的元素。
empty() – 返回队列是否为空。

用两个栈实现,分为 入栈 和 出栈 。
push:将元素推入 入栈 中。
pop:如果 出栈 是空的,那么把 入栈 中的所有元素都一一pop出来,并且按照pop顺序 push到 出栈中(这样在 出栈 中,顺序就是先进先出)。然后再pop栈顶元素。
peek:直接调用刚刚实现的pop函数,但是注意,pop是真的会把元素出栈,所以一定要记得把出栈的元素push回来
empty:只有 入栈和出栈都是空的才返回true。

本题难点

对于栈和队列方法的掌握。

代码

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 result = this->pop();
        stOut.push(result);
        return result;
    }
    
    bool empty() {
        if(stOut.empty() && stIn.empty()){
            return true;
        }else{
            return false;
        }
    }
};

/**
 * Your MyQueue object will be instantiated and called as such:
 * MyQueue* obj = new MyQueue();
 * obj->push(x);
 * int param_2 = obj->pop();
 * int param_3 = obj->peek();
 * bool param_4 = obj->empty();
 */

225. 用队列实现栈

题目链接

(1)文字讲解:https://programmercarl.com/0225.用队列实现栈.html#思路
(2)视频讲解:
(3)题目链接:https://leetcode.cn/problems/implement-stack-using-queues/description/

看到题解之前的想法

不会写

看到题解之后的想法

原来这么简单粗暴
基础版:使用两个队列
void push(int x) 队列1 push即可。
int pop() 由于队列是先进先出,所以想要pop的元素其实在队尾。所以先用队列1的pop将前面n-1个元素都pop出去放到队列2存着,然后剩下的一个元素就是栈要pop出去的元素,返回它然后pop出去就好!最后让队列1等于队列2,队列2清空。
int top() 栈顶元素是队尾,返回队列1的back就好。
boolean empty() 返回队列1的empty函数就好。
进阶版:使用一个队列
其实就是pop这个函数不一样,可以将前面n-1个元素一边pop一边push到队尾去,就不用额外的队列去存储了。

本题难点

掌握队列的常用函数。

代码

两个队列:

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

    }
    
    void push(int x) {
        q1.push(x);
    }
    
    int pop() {
        int s = q1.size();
        s--;
        while(s--){
            q2.push(q1.front());
            q1.pop();
        }
        int res = q1.front();
        q1.pop();
        q1 = q2;
        while(!q2.empty()){
            q2.pop();
        }
        return res;
    }
    
    int top() {
        return q1.back();
    }
    
    bool empty() {
        return q1.empty();
    }
};

/**
 * 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();
 */

一个队列:

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

    }
    
    void push(int x) {
        q1.push(x);
    }
    
    int pop() {
        int s = q1.size();
        s--;
        while(s--){
            q1.push(q1.front());
            q1.pop();
        }
        int res = q1.front();
        q1.pop();
        return res;
    }
    
    int top() {
        return q1.back();
    }
    
    bool empty() {
        return q1.empty();
    }
};

/**
 * 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();
 */
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值