leetcode-225-用队列实现栈
Posted oldby
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了leetcode-225-用队列实现栈相关的知识,希望对你有一定的参考价值。
题目描述:
from collections import deque class MyStack: def __init__(self): """ Initialize your data structure here. """ self.queue = deque() def push(self, x: int) -> None: """ Push element x onto stack. """ self.queue.append(x) for _ in range(len(self.queue)-1): self.queue.append(self.queue.popleft()) def pop(self) -> int: """ Removes the element on top of the stack and returns that element. """ return self.queue.popleft() def top(self) -> int: """ Get the top element. """ if self.queue: return self.queue[0] def empty(self) -> bool: """ Returns whether the stack is empty. """ return not bool(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()
以上是关于leetcode-225-用队列实现栈的主要内容,如果未能解决你的问题,请参考以下文章