225. Implement Stack using Queues(python+cp)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_21275321/article/details/83502724

题目:

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

总结:
这种设计题还是要好好学一下的。

猜你喜欢

转载自blog.csdn.net/qq_21275321/article/details/83502724