LeetCode 225用队列实现栈

LeetCode 225用队列实现栈Easy

  • 题目简述:使用队列实现栈的下列操作
    • push(x) – 元素 x 入栈
    • pop() – 移除栈顶元素
    • top() – 获取栈顶元素
    • empty() – 返回栈是否为空
  • 思路:双队列实现栈
    • 入栈实现:无条件将新元素加入队列A
    • 出栈实现:判断队列A中元素的个数是否为1,如果等于1,则出队列,否则将队列A中的元素依次出队列并放入队列B,直到队列A中的元素留下一个,然后队列A出队列,再把队列B中的元素出队列依次放入队列A中。循环往复。
class MyStack {
public:
    /** Initialize your data structure here. */
    MyStack() {

    }
    
    /** Push element x onto stack. */
    void push(int x) {
        que1.push(x);
        topval = x;
    }
    
    /** Removes the element on top of the stack and returns that element. */
    int pop() {
        if(que1.empty())
        {
            if(que2.empty()) return -1;
            while(que2.size() != 1)
            {
                topval = que2.front();
                que1.push(que2.front());
                que2.pop();
            }
            int t = que2.front();
            que2.pop();
            return t;
        }
        else
        {
            while(que1.size() != 1)
            {
                topval = que1.front();
                que2.push(que1.front());
                que1.pop();
            }
            int tt = que1.front();
            que1.pop();
            return tt;
        }
    }
    
    /** Get the top element. */
    int top() {
        return topval;
    }
    
    /** Returns whether the stack is empty. */
    bool empty() {
        if(que1.empty() && que2.empty()) return true;
        else return false;
    }
private:
    queue<int> que1;
    queue<int> que2;
    int topval;
};

/**
 * 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:
    /** Initialize your data structure here. */
    MyStack() {

    }
    
    /** Push element x onto stack. */
    void push(int x) {
        pushq.push(x);
        topval = x;
    }
    
    /** Removes the element on top of the stack and returns that element. */
    int pop() {
        while(pushq.size() > 1)
        {
            topval = pushq.front();
            popq.push(pushq.front());
            pushq.pop();
        }
        int res = pushq.front();
        pushq.pop();
        //用一个临时队列交换弹出队列和压入队列,
        //始终保持压入队列的元素个数就是留在栈的元素个数
        queue<int> tmpq = pushq;
        pushq = popq;
        popq = tmpq;
        return res;
    }
    
    /** Get the top element. */
    int top() {
        return topval;
    }
    
    /** Returns whether the stack is empty. */
    bool empty() {
        return pushq.empty();
    }
private:
    queue<int> pushq;
    queue<int> popq;
    int topval;
};

/**
 * 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:
    /** 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 res = que.front();
        que.pop();
        return res;
    }
    
    /** 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();
 */
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值