标签:removes void whether element 调用 一个栈 size code ons
使用栈实现队列的下列操作:
示例:
MyQueue queue = new MyQueue(); queue.push(1); queue.push(2); queue.peek(); // 返回 1 queue.pop(); // 返回 1 queue.empty(); // 返回 false
说明:
push to top
, peek/pop from top
, size
, 和 is empty
操作是合法的。from collections import deque class MyQueue: def __init__(self): """ Initialize your data structure here. """ self.data=deque() def push(self, x): """ Push element x to the back of queue. :type x: int :rtype: void """ return self.data.append(x) def pop(self): """ Removes the element from in front of queue and returns that element. :rtype: int """ return self.data.popleft() def peek(self): """ Get the front element. :rtype: int """ x = self.data.popleft() self.data.appendleft(x) return x def empty(self): """ Returns whether the queue is empty. :rtype: bool """ try: x = self.data.pop() self.data.append(x) return False except Exception as e: return True # Your MyQueue object will be instantiated and called as such: # obj = MyQueue() # obj.push(x) # param_2 = obj.pop() # param_3 = obj.peek() # param_4 = obj.empty()
标签:removes void whether element 调用 一个栈 size code ons
原文地址:https://www.cnblogs.com/flashBoxer/p/9527467.html