DAY10| 栈与队列

DAY9 的KMP过几天发。。等我3号考完试必写完

232.用栈实现队列

这题刷过

class MyQueue {
private:
    stack<int> s1;
    stack<int> s2;
public:
    MyQueue() {

    }//先置空?
    
    void push(int x) {
        while(s2.empty()==0){
            int t=s2.top();
            s2.pop();
            s1.push(t);
        }
        s1.push(x);
        while(s1.empty()==0){
            int t=s1.top();
            s1.pop();
            s2.push(t);
        }
    }
    
    int pop() {
        while(s2.empty()==0){
            int t=s2.top();
            s2.pop();
            s1.push(t);
        }
        while(s1.empty()==0){
            int t=s1.top();
            s1.pop();
            s2.push(t);
        }
        int t=s2.top();
        s2.pop();
        return t;
    }
    
    int peek() {
        while(s2.empty()==0){
            int t=s2.top();
            s2.pop();
            s1.push(t);
        }
        
        while(s1.empty()==0){
            int t=s1.top();
            s1.pop();
            s2.push(t);
        }
        return s2.top();
    }
    
    bool empty() {
        while(s2.empty()==0){
            int t=s2.top();
            s2.pop();
            s1.push(t);
        }
        while(s1.empty()==0){
            int t=s1.top();
            s1.pop();
            s2.push(t);
        }
        return s2.empty()==0 ? false : true;
    }
};

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

一遍AC了,隐约记得第一次刷的时候看题解发现基本上每个函数之前都会有一个把s1和s2“倒腾”一遍的过程。

class MyQueue {
private:
    stack<int> s1;
    stack<int> s2;
public:
    MyQueue() {

    }//先置空?
    
    void push(int x) {
        while(s2.empty()==0){
            int t=s2.top();
            s2.pop();
            s1.push(t);
        }
        s1.push(x);
        while(s1.empty()==0){
            int t=s1.top();
            s1.pop();
            s2.push(t);
        }
    }
    
    int pop() {
        
        int t=s2.top();
        s2.pop();
        return t;
    }
    
    int peek() {
        
        return s2.top();
    }
    
    bool empty() {
        
        return s2.empty()==0 ? false : true;
    }
};

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

删掉大部分发现也能AC。。。

没啥要说的,下一题

225. 用队列实现栈

class MyStack {
private:
    queue<int> q1,q2;//平时就放在q1里,取的时候用q2辅助
public:
    MyStack() {}
    
    void push(int x) {
        q1.push(x);
    }
    
    int pop() {
        while(q1.size()!=1){
            int t=q1.front();
            q1.pop();
            q2.push(t);
        }
        int t=q1.front();
        q1.pop();
        while(q2.empty()==0){
            int f=q2.front();
            q2.pop();
            q1.push(f);
        }
        return t;
    }
    
    int top() {
        while(q1.size()!=1){
            int t=q1.front();
            q1.pop();
            q2.push(t);
        }
        int t=q1.front();
        q1.pop();
        q2.push(t);
        while(q2.empty()==0){
            int f=q2.front();
            q2.pop();
            q1.push(f);
        }
        return t;
    }
    
    bool empty() {
        if(q1.size()!=0) return false;
        return true;
    }
};

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

AC了
另一个队列是用来备份的,因为把数据在两个队列之间“倒腾”也无法改变“先入先出”的这特点。

就是这里:
int top() {
return que1.back();
}
原来还可以这样写。
好了 栈的part1 over

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值