正直なところ、この質問は少し難解です。どの機能が使用可能でどの機能が使用できないかは明確ではありません。
後に彼はデータ構造の特徴について不明確であることが発見されました。
注:
スタックは先入れ先出しです。
キューは後入れ先出しです。
したがって、キューを使用してスタックを実装するには、1。pop
(0)(pop [0])
2. append(enter to [-1])のみを使用できます。
class MyStack:
def __init__(self):
"""
Initialize your data structure here.
"""
self.stack = []
self.pre = []
self.pre2 = []
def push(self, x: int) -> None:
"""
Push element x onto stack.
"""
#templist = [x]
#self.stack = templist + self.stack
self.stack.append(x)
def pop(self) -> int:
"""
Removes the element on top of the stack and returns that element.
"""
self.stack = self.stack[::-1]
res = self.stack.pop(0)
self.stack = self.stack[::-1]
return res
def top(self) -> int:
"""
Get the top element.
"""
print(self.stack)
return self.stack[-1]
def empty(self) -> bool:
"""
Returns whether the stack is empty.
"""
print(self.stack)
return True if self.stack == [] else False