LeetCode225:Implement Stack using Queues

Implement the following operations of a stack using queues.

  • push(x) – Push element x onto stack.
  • pop() – Removes the element on top of the stack.
  • top() – Get the top element.
  • empty() – Return whether the stack is empty.

Notes:

  • You must use only standard operations of a queue – which means only
    push to back, peek/pop from front, size, and is empty operations are
    valid.
  • Depending on your language, queue may not be supported natively. You
    may simulate a queue by using a list or deque (double-ended queue),
    as long as you use only standard operations of a queue. You may
    assume that all operations are valid (for example, no pop or top
    operations will be called on an empty stack).

Update (2015-06-11):
The class name of the Java function had been updated to MyStack instead of Stack.

Credits:
Special thanks to @jianchao.li.fighter for adding this problem and all test cases.

Hide Tags Stack Data Structure

这道题是让我们用队列来实现一个栈。
并且只使用队列中的push(),pop(),front(),empty(),size()函数。
关于队列的介绍,可以查看cpluscplus中queue的介绍。队列是一种先进先出(FIFO,first-in first-out)的容器适配器,元素被插入到容器的尾端并且从另一端弹出。

队列是作为容器适配器来实现的,容器适配器是指封装了一个特定的容器类的对象作为底层数据结构的类,它提供了一些特殊的操作来访问它的元素。元素被插入容器的尾端(back),并且从它的前端(front)弹出。

因为之前见过使用栈来实现队列,它是使用两个栈来实现队列,于是就想能不能使用两个队列来实现栈,结果发现还真是可以。定义两个队列作为自定义栈的成员变量,当需要插入元素时找到不为空的那个队列将元素插入进行,弹出元素时将不为空的那个队列的前n-1(n为队列中元素的大小)的元素弹出并插入为空的那个队列中,再弹出最后一个元素,这样插入的时间复杂度是O(1),弹出最后一个元素和获取最后一个元素的时间复杂度是O(n),图示如下:
LeetCode225:Implement Stack using Queues_第1张图片
C++代码如下:
runtime:0ms

class Stack {
public:
    // Push element x onto stack.
    void push(int x) {
        if(queueOne.empty())
        {
            queueTwo.push(x);
        }else
        {
            queueOne.push(x);
        }
    }

    // Removes the element on top of the stack.
    void pop() {
        if(queueOne.empty())
        {
            while(queueTwo.size()!=1)
            {
                queueOne.push(queueTwo.front());
                queueTwo.pop();
            }
            queueTwo.pop();
        }
        else
        {
            while(queueOne.size()!=1)
            {
                queueTwo.push(queueOne.front());
                queueOne.pop();
            }
            queueOne.pop();
        }
    }

    // Get the top element.
    int top() {
        if(queueOne.empty())
        {
            while(queueTwo.size()!=1)
            {
                queueOne.push(queueTwo.front());
                queueTwo.pop();
            }
            int result=queueTwo.front();
            queueOne.push(queueTwo.front());
            queueTwo.pop();
            return result;
        }
        else
        {
            while(queueOne.size()!=1)
            {
                queueTwo.push(queueOne.front());
                queueOne.pop();
            }
            int result=queueOne.front();
            queueTwo.push(queueOne.front());
            queueOne.pop();
            return result;
        }
    }

    // Return whether the stack is empty.
    bool empty() {
        return queueOne.empty()&&queueTwo.empty();
    }

    private:
    queue<int> queueOne;
    queue<int> queueTwo;
};

但是在Discuss中看到一种更好的解法,它只需要使用一个队列就可以了。并且代码量大大减少了。

具体做法每往队列中插入一个元素,都将它前面的元素弹出并重新插入队列中,这样就能保证最后插入队列的元素始终在队列的最前端,比如插入a,b,c,d这四个元素,队列中元素的顺序依次为a,ab,abc,abcd,这样插入的时间复杂度是O(n),弹出和获取最后一个元素的时间复杂度是O(1),是不是很奇妙?图示如下:

C++代码如下:
runtime:0ms
LeetCode225:Implement Stack using Queues_第2张图片

class Stack {
public:
    // Push element x onto stack.
    void push(int x) {
        q.push(x);
        for(int i=0;i1;i++)
        {
            q.push(q.front());
            q.pop();
        }
    }

    // Removes the element on top of the stack.
    void pop() {
        q.pop();
    }

    // Get the top element.
    int top() {
        return q.front();
    }

    // Return whether the stack is empty.
    bool empty() {
        return q.empty();
    }

    private:
    queue<int> q;
};

你可能感兴趣的:(LeetCode,LeetCode)