1. 程式人生 > >225. Implement Stack using Queues(python+cp)

225. Implement Stack using Queues(python+cp)

題目:

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.
Example:

MyStack stack = new MyStack();
stack.push(1); 
stack.push(2);   
stack.top();   // returns 2
stack.pop();   // returns 2 
stack.empty(); // returns false 

Notes:
1.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.
2.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.
3.You may assume that all operations are valid (for example, no pop or top operations will be called on an empty stack).

解釋:
用佇列來實現棧。核心思想就是不斷地pop(),然後不斷地append()
python程式碼:

class MyStack(object):

    def __init__(self):
        """
        Initialize your data structure here.
        """
        self._queue=[]
    def push(self, x):
        """
        Push element x onto stack.
        :type x: int
        :rtype: void
        """
self._queue.append(x) def pop(self): """ Removes the element on top of the stack and returns that element. :rtype: int """ #直接把最後一個元素彈出即可,不需要再append了,所以是len()-1 for i in xrange(len(self._queue)-1): self._queue.append(self._queue.pop(0)) return self._queue.pop(0) def top(self): """ Get the top element. :rtype: int """ #這樣做是為了不改變queue本身的結構 top=None for i in xrange(len(self._queue)): top=self._queue.pop(0) self._queue.append(top) return top def empty(self): """ Returns whether the stack is empty. :rtype: bool """ return self._queue==[] # Your MyStack object will be instantiated and called as such: # obj = MyStack() # obj.push(x) # param_2 = obj.pop() # param_3 = obj.top() # param_4 = obj.empty()

c++程式碼:

#include<queue>
using namespace std;
class MyStack {
public:
    /** Initialize your data structure here. */
    queue<int>_queue;
    MyStack() {
        
    }
    
    /** Push element x onto stack. */
    void push(int x) {
        _queue.push(x);
    }
    
    /** Removes the element on top of the stack and returns that element. */
    int pop() {
        for (int i=0;i<_queue.size()-1;i++)
        {
            int tmp=_queue.front();
            _queue.pop();
            _queue.push(tmp);
            
        }
        int tmp=_queue.front();
        _queue.pop();
        return tmp;
    }
    
    /** Get the top element. */
    int top() {
        int tmp=0;
        for (int i=0;i<_queue.size();i++)
        {
            tmp=_queue.front();
            _queue.pop();
            _queue.push(tmp);
        }
        return tmp;
        
    }
    
    /** Returns whether the stack is empty. */
    bool empty() {
        return _queue.empty();
    }
};

/**
 * 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();
 */

總結:
這種設計題還是要好好學一下的。