当前位置:网站首页>leetcode:232. Realize queue with stack [two stacks, one auxiliary and one simulated queue]
leetcode:232. Realize queue with stack [two stacks, one auxiliary and one simulated queue]
2022-06-29 16:04:00 【Review of the white speed Dragon King】

analysis
st1 Keeping the top of the stack is opposite to the top , In this case, it needs to be reversed twice
That is to say st1 Accept a new one at a time x
Pour it all in first st2, then st2 Again append x
Finally, put st2 Pour it all in st1
ac code
class MyQueue:
def __init__(self):
self.st1 = [] # From the top of the stack to the bottom of the stack is from the head of the queue to the end of the queue
self.st2 = [] # Auxiliary stack
def push(self, x: int) -> None:
if not self.st1:
self.st1.append(x)
else:
# hold st1 Come back
while self.st1:
self.st2.append(self.st1.pop())
self.st2.append(x) # To the end
# Back again st1, become queue
while self.st2:
self.st1.append(self.st2.pop())
#print(len(self.st1))
def pop(self) -> int:
#print(len(self.st1))
return self.st1.pop()
def peek(self) -> int:
return self.st1[-1]
def empty(self) -> bool:
return len(self.st1) == 0
# 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()
summary
Double stack implementation queue
边栏推荐
- 关于 国产麒麟系统运行Qt,在命令行可以运行而双击无法运行(无反应) 的解决方法
- 为Golang项目编写Makefile
- Pre war minesweeping: five measures for vulnerability management
- C. Where‘s the Bishop?
- 【云原生】Nacos-TaskManager 任务管理的使用
- Stlink troubleshooting
- 微博评论高可用高性能计算架构
- LeetCode-234-回文链表
- C # learning 1: value type and reference type
- Kotlin annotation declaration and use
猜你喜欢

Google software version experience cycle

It is expected to significantly improve the computational performance of integrated photonic circuits. The Tsinghua team proposed a diffraction pattern neural network framework

架构实战营模块五作业

京东联盟API - 万能转链接口 - 京品库接口 - 接口定制

Cmake learning-2

关于 麒麟系统启动应用报错“undefined symbol: __cxa_throw_bad_array_new_length, version Qt_5“ 的解决方法

【大家的项目】 Rbatis ORM官网上线

three. JS and Gaode map are combined to introduce obj format model - effect demonstration

企业转型升级之道:数字化转型,思想先行
[data analysis] five common questions about learning SQL?
随机推荐
【Try to Hack】XML
如何在你的 wordpress 网站中添加搜索框
R语言plotly可视化:plotly可视化箱图、可视化多个分类变量的箱图(Several Box Plots)
C. Most Similar Words
Three development trends of enterprise application viewed from the third technological revolution
华为云AOM 2.0版本发布
A. Marathon
Daily / June 29, 2022: where is Li Feifei's focus on "embodied intelligence"?
Andorid Jetpack Hilt
A. Print a Pedestal (Codeforces logo?)
golang操作etcd
mysql报错:Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column
STM32与GD32笔记
CVPR 2022 | greatly reduce the manual annotation required for zero sample learning. Mapuosuo and Beiyou proposed category semantic embedding rich in visual information
2022 OpenVINO DevCon 大揭秘!英特尔携众多合作伙伴深化开发者生态建设,释放AI产业创新潜能
Training mode of deep learning network
MySQL常用语句和命令汇总
为Golang项目编写Makefile
攻防演练之战前扫雷:漏洞管理的5大措施
关于 国产麒麟系统运行Qt,在命令行可以运行而双击无法运行(无反应) 的解决方法