【LEETCODE】225-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 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 to back(加入队尾),pop from front(弹出队首),size(取队列大小)以及is empty(判断是否为空)


就是只能用这几个操作,不能再用其他现成的操作


class Stack(object):
    def __init__(self):
        """
        initialize your data structure here.
        """
        self.queue=[]

    def push(self, x):
        """
        :type x: int
        :rtype: nothing
        """
        self.queue.append(x)

    def pop(self):
        """
        :rtype: nothing
        """
        for x in range(len(self.queue)-1):
            self.queue.append(self.queue.pop())
        self.queue.pop()

    def top(self):
        """
        :rtype: int
        """
        top=None
        for x in range(len(self.queue)):
            top=self.queue.pop()
            self.queue.append(top)
        return top
            

    def empty(self):
        """
        :rtype: bool
        """
        return len(self.queue)==0


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