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

  • 今日学习的文章链接,或者视频链接

第五章 栈与队列part01

  • 自己看到题目的第一想法

  • 看完代码随想录之后的想法

232:

class MyQueue {
public:
    stack stIn;
    stack 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() {
        if (stOut.empty()){
            while(!stIn.empty()){
                stOut.push(stIn.top());
                stIn.pop();
            }
        }
        return stOut.top();

    }
    
    bool empty() {
        return stIn.empty() && stOut.empty();
    }
};

/**
 * 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:

class MyStack {
public:
    queue q;
    MyStack() {

    }
    
    void push(int x) {
        q.push(x);
    }
    
    int pop() {
        int n = q.size();
        for (int i =1;ipush(x);
 * int param_2 = obj->pop();
 * int param_3 = obj->top();
 * bool param_4 = obj->empty();
 */
  • 自己实现过程中遇到哪些困难

  • 今日收获,记录一下自己的学习时长

你可能感兴趣的:(算法,java,前端)