zoukankan      html  css  js  c++  java
  • 232. 用栈实现队列

    232. 用栈实现队列

    题目链接:232. 用栈实现队列(简单)

    题目描述

    请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作(push、pop、peek、empty):

    实现 MyQueue 类:

    • void push(int x) 将元素 x 推到队列的末尾

    • int pop() 从队列的开头移除并返回元素

    • int peek() 返回队列开头的元素

    • boolean empty() 如果队列为空,返回 true ;否则,返回 false

    说明:

    • 你只能使用标准的栈操作 —— 也就是只有 push to top, peek/pop from top, size, 和 is empty 操作是合法的。

    • 你所使用的语言也许不支持栈。你可以使用 list 或者 deque(双端队列)来模拟一个栈,只要是标准的栈操作即可。

    进阶:

    • 你能否实现每个操作均摊时间复杂度为 O(1) 的队列?换句话说,执行 n 个操作的总时间复杂度为 O(n) ,即使其中一个操作可能花费较长时间。

    示例:

    输入:
    ["MyQueue", "push", "push", "peek", "pop", "empty"]
    [[], [1], [2], [], [], []]
    输出:
    [null, null, null, 1, 1, false]

    解释:
    MyQueue myQueue = new MyQueue();
    myQueue.push(1); // queue is: [1]
    myQueue.push(2); // queue is: [1, 2] (leftmost is front of the queue)
    myQueue.peek(); // return 1
    myQueue.pop(); // return 1, queue is [2]
    myQueue.empty(); // return false

    提示:

    • 1 <= x <= 9

    • 最多调用 100 次 push、pop、peek 和 empty

    • 假设所有操作都是有效的 (例如,一个空的队列不会调用 pop 或者 peek 操作)

    题解

    思路:栈遵循的是”先进后出“原则,而队列是”先进先出“,所以此题需要用到一个输入栈和一个输出栈。对于push(),只需将元素推入输入栈即可。对于pop()和peak(),首先需要判断当前的输出栈是否为空,如果不为空,直接弹出或返回输出栈的栈顶元素即可;如果输出栈为空,需要将输入栈里面的所有元素按顺序弹出并推入到输出栈中,再弹出或返回输出栈的栈顶元素。对于empty(),只有输入栈和输出栈都为空时,才返回true。

    代码(C++)

    class MyQueue {
    public:
        stack<int> staIn;//输入栈
        stack<int> staOut;//输出栈
    ​
        MyQueue() {
    ​
        }
    ​
        void push(int x) {
            staIn.push(x);
        }
    ​
        int pop() {
            //如果输出栈为空,输入栈非空,则要将输入栈的元素全部push进输出栈
            if (staOut.empty()) {
                while (!staIn.empty()) {
                    staOut.push(staIn.top());
                    staIn.pop();
                }
            }
            int result = staOut.top();
            staOut.pop();
            return result;
        }
    ​
        int peek() {
    //        if (staOut.empty()) {
    //            while (!staIn.empty()) {
    //                staOut.push(staIn.top());
    //                staIn.pop();
    //            }
    //        }
    //        int result = staOut.top();
    //        return result;
            int result = this->pop(); // 使用已有的pop函数
            staOut.push(result); // 因为pop函数弹出了元素result,所以需要再添加回去
            return result;
        }
    ​
        bool empty() {
            if (staIn.empty() && staOut.empty()) return true;
            else return false;
        }
    };

     分析:

    • 时间复杂度:push(): O(1);pop(): O(N);peek(): O(N);empty(): O(1)。N为栈中的元素个数

    • 空间复杂度:O(N)

     

     

     

  • 相关阅读:
    CodeForces 734F Anton and School
    CodeForces 733F Drivers Dissatisfaction
    CodeForces 733C Epidemic in Monstropolis
    ZOJ 3498 Javabeans
    ZOJ 3497 Mistwald
    ZOJ 3495 Lego Bricks
    CodeForces 732F Tourist Reform
    CodeForces 732E Sockets
    CodeForces 731E Funny Game
    CodeForces 731D 80-th Level Archeology
  • 原文地址:https://www.cnblogs.com/wltree/p/15532332.html
Copyright © 2011-2022 走看看