栈和队列

1.用两个栈实现队列

232.Implement Queue using Stacks

Implement the following operations of a queue using stacks.

  • push(x) -- Push element x to the back of queue.
  • pop() -- Removes the element from in front of queue.
  • peek() -- Get the front element.
  • empty() -- Return whether the queue is empty.

Notes:

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

Solution

class Queue(object):
    def __init__(self):
        """
        initialize your data structure here.
        """
        self.input = []  # stack, only append,pop are allowed
        self.output = []

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

    def pop(self):
        """
        :rtype: nothing
        """
        self.peek
        return self.output.pop()

    def peek(self):
        """
        :rtype: int
        """
        if self.output == []:
            while self.input != []:
                self.output.append(self.input.pop())
        return self.output[-1]

    def empty(self):
        """
        :rtype: bool
        """
        return len(self.input) == 0 and len(self.output) == 0     

2.用两个队列实现栈

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 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).

Solution

class Stack(object):
    # initialize your data structure here.
    def __init__(self):
        self.queue1 = []
        self.queue2 = []
        self.size = 0
    
    # @param x, an integer
    # @return nothing
    def push(self, x):
        if not self.queue2:
            self.queue1.append(x)
        else:
            self.queue2.append(x)
        self.size += 1
    
    # @return nothing
    def pop(self):
        if not self.queue2:
            for _ in xrange(self.size-1):
                self.queue2.append(self.queue1.pop(0))
            self.queue1.pop(0)
        else:
            for _ in xrange(self.size-1):
                self.queue1.append(self.queue2.pop(0))
            self.queue2.pop(0)
        self.size -= 1
    
    # @return an integer
    def top(self):
        if not self.queue2:
            for _ in xrange(self.size-1):
                self.queue2.append(self.queue1.pop(0))
            tmp = self.queue1.pop(0)
            self.queue2.append(tmp)
            return tmp
        else:
            for _ in xrange(self.size-1):
                self.queue1.append(self.queue2.pop(0))
            tmp = self.queue2.pop(0)
            self.queue1.append(tmp)
            return tmp
    
    # @return an boolean
    def empty(self):
        return self.size == 0
using one queue
class Stack(object):
    def __init__(self):
        """
        initialize your data structure here.
        """
        import collections
        self.queue = collections.deque()

    def push(self, x):
        """
        :type x: int
        :rtype: nothing
        """
        q = self.queue
        q.append(x)
        for i in range(len(q)-1):
            q.append(q.popleft())

    def pop(self):
        """
        :rtype: nothing
        """
        self.queue.popleft()

    def top(self):
        """
        :rtype: int
        """
        return self.queue[0]

    def empty(self):
        """
        :rtype: bool
        """
        return not len(self.queue)
        
原文地址:https://www.cnblogs.com/binwone/p/6083103.html