[Leetcode] 225. Implement Stack using Queues 解题报告

来源:互联网 发布:ssh传输文件命令 mac 编辑:程序博客网 时间:2024/06/03 15:37

题目

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 backpeek/pop from frontsize, 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).

思路

我们首先看看队列和栈的区别:队列是先进先出,而栈是后进先出。可以看出算法实现的关键就在于在队列中存储元素的时候,将最近push进来的元素放在队列首位还是末尾了。根据这个区别,就可以有两种实现方法:

1、懒惰实现:顾名思义,就是新加入一个元素之后,我们懒惰第将它放在队列末尾。这就导致一个问题,当需要pop的时候,我们此时需要pop掉的是队列中的最后一个元素,而这个操作是队列这种数据结构所不支持的。所以在懒惰实现的pop中,我们需要首先把除队末元素之外的所有队列元素都移出队列,然后重新添加,这样就可以保证原来的队末元素此时位于队列头部了,可以直接pop。

2、勤奋实现:顾名思义,就是新push一个元素进来的时候,我们想办法把它置于队列头部,这样当需要pop的时候直接pop出来队列头部就可以了。所以这种“勤奋实现”的关键在于push函数的实现。事实上它和“懒惰实现”的pop函数完全一样:先把新元素加在队列元素的末尾,然后把所有除此之外的元素顺次移除,并添加到队列的末尾。

代码

1、懒惰实现:

class MyStack {public:    /** 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();        for (int i = 0; i < size - 1; ++i) {            que.push(que.front());            que.pop();        }        int ret = que.front();        que.pop();        return ret;    }        /** Get the top element. */    int top() {        return que.back();    }        /** Returns whether the stack is empty. */    bool empty() {        return que.empty();    }private:    queue<int> que;};/** * 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(); */

2、勤奋实现:

class MyStack {public:    /** Initialize your data structure here. */    MyStack() {            }        /** Push element x onto stack. */    void push(int x) {        que.push(x);        for (int i = 0; i < que.size() - 1; ++i) {            que.push(que.front());            que.pop();        }    }        /** Removes the element on top of the stack and returns that element. */    int pop() {        int ret = que.front();        que.pop();        return ret;    }        /** Get the top element. */    int top() {        return que.front();    }        /** Returns whether the stack is empty. */    bool empty() {        return que.empty();    }private:    queue<int> que;};/** * 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(); */
原创粉丝点击