代码随想day10 | 232.用栈实现队列、 225. 用队列实现栈

 232.用栈实现队列 

这道题的思路是使用两个栈A,B,两个先进后出的栈能实现先进先出的功能,其中A是缓存栈,B是输出栈,维护这两个栈即可。push时直接push到A中;pop和peak时候如果B不为空,直接操作B;如果为空,则把A中所有元素压栈到B,然后再操作B。

详细代码如下:

class MyQueue {
public:
    MyQueue() {
        ;
    }
    
    void push(int x) {
        A.push(x);
    }
    
    int pop() {
        if(!B.empty())
        {
            int tmp = B.top();
            B.pop();
            return tmp;
        }
        else
        {
            while(!A.empty())
            {
                int tmp = A.top();
                A.pop();
                B.push(tmp);
            }
            int tmp = B.top();
            B.pop();
            return tmp;
        }
        return -1;
    }
    
    int peek() {
        if(!B.empty())
        {
            return B.top();
        }
        else
        {
            while(!A.empty())
            {
                int tmp = A.top();
                A.pop();
                B.push(tmp);
            }
            int tmp = B.top();
            return tmp;
        }
        return -1;
    }
    
    bool empty() {
        if(A.empty()&&B.empty()) return true;
        else return false;

    }
private:
    stack<int> A;
    stack<int> B;

};

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

学习代码随想录后学到一个技巧,peak和pop高度相似,所以peak可以复用pop的代码,使用this指针,可以避免写大量重复的代码,具体如下:

    int peek() {

        int tmp = this->pop();

        B.push(tmp);  //把顶部pop的数据再压入即可

        return tmp;

    }

225. 用队列实现栈 

自己的思路:A是输入队列,B是输出队列,输出队列永远是空的,输入就pushA,pop的时候,就找到A最后一个元素,最后一个元素就是队尾,不需要pop到B,其余的都pop到B,再把AB队列交换,这样B输出队列永远是空的,而top只需要复用pop就行(复杂度高)。

详细代码:

class MyStack {
public:
    queue<int> A;
    queue<int> B;
    MyStack() {
        ;
    }
    
    void push(int x) {
        A.push(x);
    }
    
    int pop() {
        int tmp;
        if(!B.empty())
        {
            tmp = B.front();
            B.pop();
            
        }
        else
        {
            while(!A.empty())
            {
            tmp = A.front();
            A.pop();
            if(!A.empty()) B.push(tmp);
            }
            swap(A,B);

        }
        return tmp;

    }
    
    int top() {
        int tmp = this->pop();
        A.push(tmp);
        return tmp;
    }
    
    bool empty() {
        if(A.empty()&&B.empty()) return true;
        else return false;
    }
};

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

进阶:但是感觉自己实现的这种思路非常别扭,学习代码随想录的思路,发现queue这种容器适配器是有size函数和back函数的,这样代码就简单了,只需要维护一个队列即可,直接把size-1个元素pop再push到尾部,这样第一个就是最后一个进入的元素,代码如下:除了pop的复杂度是O(n),其他操作都是O(1).

class MyStack {
public:
    queue<int> A;
    MyStack() {
        ;
    }
    
    void push(int x) {
        A.push(x);
    }
    
    int pop() {
        int n=A.size()-1;
        while(n>0)
        {
            int top = A.front();
            A.pop();
            A.push(top);
            n--;
        }
        int tmp = A.front();
        A.pop();
        return tmp;
    }
    
    int top() {
        int tmp = this->pop();
        A.push(tmp);
        return tmp;
    }
    
    bool empty() {
        return A.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();
 */

  • 5
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值