当前位置:网站首页>剑指Offer(二十一):栈的压入、弹出序列
剑指Offer(二十一):栈的压入、弹出序列
2022-07-26 10:35:00 【康斯坦奇】
解法1:借用一个辅助栈,遍历压栈顺序,先将第一个放入栈中,这里是1,然后判断栈顶元素是不是出栈顺序的第一个元素,这里是4,很显然1!=4,所以我们继续压栈,直到相等以后开始出栈,出栈一个元素,则将出栈顺序向后移动一位,直到不相等,这样循环等压栈顺序遍历完成,如果辅助栈还不为空,说明弹出序列不是该栈的弹出顺序!
class Solution:
def IsPopOrder(self, pushV, popV):
# write code here
if len(popV) == 0 or len(pushV) != len(popV):
return False
stackData = []
for i in pushV:
stackData.append(i)#借用辅助栈,遍历压栈顺序!
while len(stackData) and stackData[-1] == popV[0]:
stackData.pop()
popV.pop(0)
if len(stackData):
return False
return True
边栏推荐
- Introduction to data analysis | kaggle Titanic mission
- 12 复制对象时勿忘其每一个成分
- .net operation redis list list
- Inheritance method of simplified constructor (I) - combined inheritance
- 图片随手机水平移动-陀螺仪。360度设置条件
- 同步方法中不使用asyncTask<T> 修饰和await获取异步返回值(同步方法中调用异步方法)
- Issue 5: the second essential skill for College Students
- [Halcon vision] software programming ideas
- 事务的传播性propagation
- Zongzi battle - guess who can win
猜你喜欢

Tradingview tutorial

Issue 7: how do you choose between curling up and lying flat

QRcode二维码(C语言)遇到的问题

Centos8 (liunx) deploying WTM (asp.net 5) using PgSQL

js下载文件,FileSaver.js导出txt、excel文件
![[Halcon vision] Fourier transform of image](/img/9c/d6ed4ab3e40f706f3b5b8b5cc51db9.png)
[Halcon vision] Fourier transform of image

videojs转canvas暂停、播放、切换视频

centos8(liunx)部署WTM(ASP.NET 5)使用pgsql

【机器学习小记】【风格迁移】deeplearning.ai course4 4th week programming(tensorflow2)

Redis Docker实例与数据结构
随机推荐
Analyze the hybrid construction objects in JS in detail (construction plus attributes, prototype plus methods)
Datav beautiful data screen production experience
【论文下饭】Deep Mining External Imperfect Data for ChestX-ray Disease Screening
数据库函数
13 以对象管理资源
js,e.pageX、pageY模态框拖动
第4期:大学生提前职业技能准备之一
事务的传播性propagation
Tradingview tutorial
[leetcode每日一题2021/2/14]765. 情侣牵手
C language callback function
Summary of common skills in H5 development of mobile terminal
Inheritance method of simplified constructor (I) - combined inheritance
STM32 阿里云MQTT esp8266 AT命令
Zongzi battle - guess who can win
MD5 encryption
Some cutting-edge research work sharing of SAP ABAP NetWeaver containerization
多目标优化系列1---NSGA2的非支配排序函数的讲解
超图 影像 如何去除黑边(两种方法)
[leetcode每日一题2021/8/30]528. 按权重随机选择【中等】