232.用栈实现队列
请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作(push
、pop
、peek
、empty
):
实现 MyQueue
类:
void push(int x)
将元素 x 推到队列的末尾int pop()
从队列的开头移除并返回元素int peek()
返回队列开头的元素boolean empty()
如果队列为空,返回true
;否则,返回false
说明:
- 你 只能 使用标准的栈操作 —— 也就是只有
push to top
,peek/pop from top
,size
, 和is empty
操作是合法的。 - 你所使用的语言也许不支持栈。你可以使用 list 或者 deque(双端队列)来模拟一个栈,只要是标准的栈操作即可。
解题思路:
class MyQueue:
def __init__(self):
初始化两个stack,in和out
def push(self, x: int) -> None:
直接在stack_in添加append
def pop(self) -> int:
如果stack_out中有的话直接pop,否则pop stack_in中的元素入stack_out,然后pop
注意遍历stack_in中的元素进入stack_out时,不能使用for i in stack_in, 因为只能访问和使用列表中的元素值
def peek(self) -> int:
使用self.pop得到第一个值,再添加进stack_out
def empty(self) -> bool:
return not stack_in and not stack_out
class MyQueue:
def __init__(self):
self.stack_in = []
self.stack_out = []
def push(self, x: int) -> None:
self.stack_in.append(x)
def pop(self) -> int:
if self.stack_out:
return self.stack_out.pop()
else:
for i in range(len(self.stack_in)):# or while self.stack_in
# print(self.stack_in.pop())
self.stack_out.append(self.stack_in.pop())
return self.stack_out.pop()
def peek(self) -> int:
ans = self.pop()
self.stack_out.append(ans)
return ans
def empty(self) -> bool:
return not self.stack_in and not self.stack_out
# 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()
225. 用队列实现栈
请你仅使用两个队列实现一个后入先出(LIFO)的栈,并支持普通栈的全部四种操作(push
、top
、pop
和 empty
)。
实现 MyStack
类:
void push(int x)
将元素 x 压入栈顶。int pop()
移除并返回栈顶元素。int top()
返回栈顶元素。boolean empty()
如果栈是空的,返回true
;否则,返回false
。
解题思路:
设置两个deque,一个用来添加及操作指令,一个用来存储做备忘。注意deque一般都是从尾端加入新元素,如果想从前端加入元素,使用deque.appendleft(),其他操作与stack实现队列相似。定义pop()时,需要将stack_in的长度减一个元素从右边移走到stack_out中,执行pop(),再将所有stack_out的元素移动回stack_in.
代码如下:
class MyStack:
def __init__(self):
self.stack_in = deque()
self.stack_out = deque()
def push(self, x: int) -> None:
self.stack_in.appendleft(x)
def pop(self) -> int:
#store the previous elements except teh last one into another queue
n = len(self.stack_in)
print(self.stack_in)
for i in range(n-1):
self.stack_out.appendleft(self.stack_in.pop())
ans = self.stack_in.pop()
while self.stack_out:
self.stack_in.appendleft(self.stack_out.pop())
print(ans)
return ans
def top(self) -> int:
ans = self.pop()#apply the self.pop() function defined in this class
self.stack_in.appendleft(ans)
return ans
def empty(self) -> bool:
return not self.stack_in
# 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()
或者:
class MyStack:
def __init__(self):
self.stack_in = deque()
self.stack_out = deque()
def push(self, x: int) -> None:
self.stack_in.append(x)
def pop(self) -> int:
#store the previous elements except teh last one into another queue
n = len(self.stack_in)
# print(self.stack_in)
for i in range(n-1):
self.stack_out.append(self.stack_in.popleft())
ans = self.stack_in.pop()
while self.stack_out:
self.stack_in.append(self.stack_out.popleft())
print(ans)
return ans
def top(self) -> int:
ans = self.pop()#apply the self.pop() function defined in this class
self.stack_in.append(ans)
return ans
def empty(self) -> bool:
return not self.stack_in