前言
理论基础
这里介绍的c++的标准库底层实现逻辑
base的是数组,也就是连续空间,其实还有链式的,依据
讲解课程
232.用栈实现队列

使用栈实现队列的下列操作:
push(x) – 将一个元素放入队列的尾部。
pop() – 从队列首部移除元素。
peek() – 返回队列首部的元素。
empty() – 返回队列是否为空。
注意看题目的说明
- 告诉你了stack可以用list来做,这是提示
- 没有错误操作,说明不用考虑有问题的,例如empty的时候让它pop
思路💖
总体思路:需要两个栈一个输入栈,一个输出栈【看动图】
push:要数据放进输入栈就好
pop:输出栈如果为空,就把进栈数据全部导入进来(注意是全部导入),再从出栈弹出数据,如果输出栈不为空,则直接从出栈弹出数据就可以了。
peek:直接调用写好的pop函数,因为pop函数弹出了元素res,所以再添加回去
empty:如果进栈和出栈都为空的话,说明模拟的队列为空了。


方法一
在python中,使用栈主要是数组,使用list的append和pop作为入栈出栈;
自己写的错误点
- 如下图所示:判断一个list是否为空,不能用stack==None,直接if stack就行

- 如果使用否,python里面是用的not
- 忘了条件if,如out栈是空的才需要压栈,否则直接弹出out里面的
- 代码的优化:ele = self.stackin.pop()和 self.stackout.append(ele)这样的两句话可优化为一句话self.stackout.append(self.stackin.pop())
- 此外:not (self.stack_in or self.stack_out) 和not self.stackin and not self.stackout同义hhh
class MyQueue(object):
def __init__(self):
self.stackin = [] # python自己创建stack,用list、
self.stackout = []
def push(self, x):
"""
:type x: int
:rtype: None
"""
self.stackin.append(x)
def pop(self):
"""
:rtype: int
"""
# if self.empty():#可以省略,因为说明里面说不会有违法操作
# return None
if not self.stackout:#如果out是空的,需要压栈入out,这个一开始忘了写了
while self.stackin: # 全部弹出到stackout里面
ele = self.stackin.pop()
self.stackout.append(ele)
# pop就是显示最上面的ele的内容,并且弹出去
result = self.stackout.pop()
return result
def peek(self):
"""
:rtype: int
"""
peek_ele = self.pop() # 利用写好的pop函数
self.stackout.append(peek_ele)
return peek_ele
def empty(self):
"""
:rtype: bool
"""
return not self.stackin and not self.stackout
225. 用队列实现栈

思路
主要思想:一个队列在模拟栈弹出元素的时候只要将队列头部的元素(除了最后一个元素外) 重新添加到队列尾部,此时再去弹出元素就是栈的顺序了。
- pop和top的时候都是一样的操作,默认的状态是正常的队列的状态,所以这两个操作都是要把size–的元素放到队列的最后的。

方法一 一个队列来实现栈
知识点:python的deque是一个双向的队列,如果方向向左就是append和popleft,如果向右就是appendleft和pop对应;
下面自己写 的很简单
from collections import deque
class MyStack(object):
def __init__(self):
self.que = deque()
def push(self, x):
"""
:type x: int
:rtype: None
"""
self.que.append(x)
def pop(self):
"""
:rtype: int
"""
#容错,如果为空
if len(self.que) == 0:
return None
n = len(self.que)
for i in range(0,n-1):
self.que.append((self.que.popleft()))#popleft的加到后面
return self.que.popleft()
def top(self):
"""
:rtype: int
"""
if len(self.que) == 0:
return None
n = len(self.que)
for i in range(0,n-1):
self.que.append((self.que.popleft()))#popleft的加到后面
tmp = self.que.popleft()
self.que.append(tmp) #变成正常的队列的顺序
return tmp
def empty(self):
"""
:rtype: bool
"""
return not self.que
方法二 两个队列来实现栈
我这个没看,以后有时间补充吧;
总体思路就是把size-1的移动到另外一个队列里面;【讲解里面有动图】
from collections import deque
class MyStack:
def __init__(self):
"""
Python普通的Queue或SimpleQueue没有类似于peek的功能
也无法用索引访问,在实现top的时候较为困难。
用list可以,但是在使用pop(0)的时候时间复杂度为O(n)
因此这里使用双向队列,我们保证只执行popleft()和append(),因为deque可以用索引访问,可以实现和peek相似的功能
in - 存所有数据
out - 仅在pop的时候会用到
"""
self.queue_in = deque()
self.queue_out = deque()
def push(self, x: int) -> None:
"""
直接append即可
"""
self.queue_in.append(x)
def pop(self) -> int:
"""
1. 首先确认不空
2. 因为队列的特殊性,FIFO,所以我们只有在pop()的时候才会使用queue_out
3. 先把queue_in中的所有元素(除了最后一个),依次出列放进queue_out
4. 交换in和out,此时out里只有一个元素
5. 把out中的pop出来,即是原队列的最后一个
tip:这不能像栈实现队列一样,因为另一个queue也是FIFO,如果执行pop()它不能像
stack一样从另一个pop(),所以干脆in只用来存数据,pop()的时候两个进行交换
"""
if self.empty():
return None
for i in range(len(self.queue_in) - 1):
self.queue_out.append(self.queue_in.popleft())
self.queue_in, self.queue_out = self.queue_out, self.queue_in # 交换in和out,这也是为啥in只用来存
return self.queue_out.popleft()
def top(self) -> int:
"""
写法一:
1. 首先确认不空
2. 我们仅有in会存放数据,所以返回第一个即可(这里实际上用到了栈)
写法二:
1. 首先确认不空
2. 因为队列的特殊性,FIFO,所以我们只有在pop()的时候才会使用queue_out
3. 先把queue_in中的所有元素(除了最后一个),依次出列放进queue_out
4. 交换in和out,此时out里只有一个元素
5. 把out中的pop出来,即是原队列的最后一个,并使用temp变量暂存
6. 把temp追加到queue_in的末尾
"""
# 写法一:
# if self.empty():
# return None
# return self.queue_in[-1] # 这里实际上用到了栈,因为直接获取了queue_in的末尾元素
# 写法二:
if self.empty():
return None
for i in range(len(self.queue_in) - 1):
self.queue_out.append(self.queue_in.popleft())
self.queue_in, self.queue_out = self.queue_out, self.queue_in
temp = self.queue_out.popleft()
self.queue_in.append(temp)
return temp
def empty(self) -> bool:
"""
因为只有in存了数据,只要判断in是不是有数即可
"""
return len(self.queue_in) == 0
总结
内容很简单。。。。但是今天又是发烧又是拉肚子,还是拖到了晚上
505



被折叠的 条评论
为什么被折叠?



