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

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

232. 用栈实现队列

题目链接

提交代码()

class MyQueue {
    
    
public:
    stack<int> first;
    stack<int> second;
    MyQueue() {
    
    

    }
    
    void push(int x) {
    
    
        first.push(x);
    }
    
    int pop() {
    
    
        if(second.empty())
        {
    
    
            while(!first.empty())
            {
    
    
                second.push(first.top());
                first.pop();
            }
        }
        int res = second.top();
        second.pop();
        return res;
    }
    
    int peek() {
    
    
        int pee = this -> pop();
        second.push(pee);
        return pee;
    }
    
    bool empty() {
    
    
        return first.empty() && second.empty();
    }
};

解答代码(方法)

class MyQueue {
    
    
public:
    stack<int> stIn;
    stack<int> stOut;
    /** Initialize your data structure here. */
    MyQueue() {
    
    

    }
    /** Push element x to the back of queue. */
    void push(int x) {
    
    
        stIn.push(x);
    }

    /** Removes the element from in front of queue and returns that element. */
    int pop() {
    
    
        // 只有当stOut为空的时候,再从stIn里导入数据(导入stIn全部数据)
        if (stOut.empty()) {
    
    
            // 从stIn导入数据直到stIn为空
            while(!stIn.empty()) {
    
    
                stOut.push(stIn.top());
                stIn.pop();
            }
        }
        int result = stOut.top();
        stOut.pop();
        return result;
    }

    /** Get the front element. */
    int peek() {
    
    
        int res = this->pop(); // 直接使用已有的pop函数
        stOut.push(res); // 因为pop函数弹出了元素res,所以再添加回去
        return res;
    }

    /** Returns whether the queue is empty. */
    bool empty() {
    
    
        return stIn.empty() && stOut.empty();
    }
};

225.用队列实现栈

题目链接

提交代码(方法)

class MyStack {
    
    
public:
        queue<int> que;
    MyStack() {
    
    
        
    }
    
    void push(int x) {
    
    
        que.push(x);
    }
    
    int pop() {
    
    
        int size = que.size();
        for(int i = 0; i < size - 1; i++)
        {
    
    
            que.push(que.front());
            que.pop();
        }
        int result = que.front();
        que.pop();
        return result;
    }
    
    int top() {
    
    
        return que.back();
    }
    
    bool empty() {
    
    
        return que.empty();
    }
};

解答代码(方法)

class MyStack {
    
    
public:
    queue<int> que;
    /** Initialize your data structure here. */
    MyStack() {
    
    

    }
    /** Push element x onto stack. */
    void push(int x) {
    
    
        que.push(x);
    }
    /** Removes the element on top of the stack and returns that element. */
    int pop() {
    
    
        int size = que.size();
        size--;
        while (size--) {
    
     // 将队列头部的元素(除了最后一个元素外) 重新添加到队列尾部
            que.push(que.front());
            que.pop();
        }
        int result = que.front(); // 此时弹出的元素顺序就是栈的顺序了
        que.pop();
        return result;
    }

    /** Get the top element. */
    int top() {
    
    
        return que.back();
    }

    /** Returns whether the stack is empty. */
    bool empty() {
    
    
        return que.empty();
    }
};

总结

                     日期: 2023 年 3 月 24 日
              学习时长: 0 h 30 m
                     难度: ★ \bigstar ★ \bigstar
累计完成题目数量: 27
距离目标还有数量: 273
                      小结:

  1. 队列还能直接取队尾元素,才知道

猜你喜欢

转载自blog.csdn.net/qq_43212651/article/details/129745402