当前位置:网站首页>Sword finger offer (V): queue with two stacks
Sword finger offer (V): queue with two stacks
2022-07-26 10:45:00 【Konstanch】
solution 1: Initialize two empty stacks , When stack2 When is not empty , stay stack2 The top element in the stack is the first element to enter the queue , It can pop up . If stack2 It's empty time , We put stack1 The elements in pop up one by one and press in stack2. Because the element that enters the queue first is pressed to stack1 At the bottom of the stack , After ejecting and pressing in, it is in stack2 Top of stack , You can pop up directly . If there are new elements d Insert , We press it directly into stack1 that will do !
class Solution:
def __init__(self):
self.stack1 = []
self.stack2 = []
def push(self, node):
# write code here
return self.stack1.append(node)
def pop(self):
# return xx
if self.stack2 == []:
while self.stack1:
self.stack2.append(self.stack1.pop())
return self.stack2.pop()
return self.stack2.pop()
边栏推荐
- 鹏哥C语言第七节课总结
- 剑指Offer(四十四):翻转单词顺序序列
- list升序和降序
- [leetcode daily question 2021/2/18] [detailed explanation] minimum number of turns of 995. K continuous bits
- display-inline+calc实现左中右布局,中间自适应
- Phase 4: one of College Students' vocational skills preparation in advance
- Issue 6: which mainstream programming language should college students choose
- Build ARM embedded development environment
- Pengge C language lesson 4 (3)
- JS对象赋值问题
猜你喜欢

一文详解Nodejs中fs文件模块与path路径模块

从蚂蚁的觅食过程看团队研发(转载)
![[leetcode daily question 2021/5/8]1723. The shortest time to complete all work](/img/e7/a48bb5b8a86cbc4cd5b37bb16661a8.png)
[leetcode daily question 2021/5/8]1723. The shortest time to complete all work

2021-08-12 function recursion_ Learn C language with brother Peng

2021-08-12函数递归_和鹏哥学习C语言

RT-Thread 学习笔记(五)---编辑、下载、调试程序

二叉树的遍历 递归+迭代

在altium designer中禁用USBJATG

0x00007FFD977C04A8 (Qt5Sqld.dll)处(位于 a.exe 中)引发的异常: 0xC0000005: 读取位置 0x0000000000000010 时发生访问冲突

kali 查看ip地址
随机推荐
Flutter TextField设置高度并且自动换行,圆角边框去除下划线
Sql Server之查询总结
剑指Offer(四十四):翻转单词顺序序列
剑指Offer(四十九):把字符串转换成整数
router.push(),router.repalce(),router.go()使用
剑指Offer(五十二):正则化表达式
RT thread learning notes (VII) -- open the elmfat file system based on SPI flash (middle)
抽象工厂及其改进示例
使用float实现左中右布局,中间内容自适应
[leetcode daily question 2021/5/8]1723. The shortest time to complete all work
文案秘籍七步曲至----文献团队协作管理
WinPcap packet capturing function pcap_ Loop (), stop the problem
2021-08-12函数递归_和鹏哥学习C语言
242.有效的字母异位词
[machine learning notes] [style transfer] deeplearning ai course4 4th week programming(tensorflow2)
用两个栈实现队列
toolstrip 去边框
RT thread learning notes (VI) -- start the elmfat file system based on SPI flash (Part 1)
一文详解Nodejs中fs文件模块与path路径模块
使用Selenium抓取zabbix性能监控图